我正在制作一个解析“a.xml”的3文件,问题是因为我得到了最后一行而不是整个解析。 你能帮帮我吗?
package barc3.barc3;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class barc3 extends Activity {
private TextView orgXmlTxt;
private TextView parsedXmlTxt;
private Button parseBtn,chartbtn;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
try {
setContentView(R.layout.main);
orgXmlTxt = (TextView) findViewById(R.id.orgXMLTxt);
parsedXmlTxt = (TextView) findViewById(R.id.parsedXMLTxt);
parseBtn = (Button) findViewById(R.id.parseBtn);
chartbtn =(Button) findViewById(R.id.chartbtn);
//orgXmlTxt.setText(getOriginalMyXML());
parseBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
parsedXmlTxt.setText(getParsedMyXML());
} catch (Exception e) {
// TODO Auto-generated catch block
orgXmlTxt.setText(e.getMessage());
}
}
});
chartbtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(barc3.this,XMLParsingDemo.class));
}
});
} catch (Exception e) {
orgXmlTxt.setText(e.getMessage());
}
}
/**
* This method is called by OnCreate during startup to show the original XML file content
* @return
* @throws Exception
*/
private String getOriginalMyXML() throws Exception {
/* Load xml file being shown from raw resource folder */
InputStream in = this.getResources().openRawResource(R.raw.a);
StringBuffer inLine = new StringBuffer();
InputStreamReader isr = new InputStreamReader(in);
BufferedReader inRd = new BufferedReader(isr);
String text;
while ((text = inRd.readLine()) != null) {
inLine.append(text);
inLine.append("\n");
}
in.close();
return inLine.toString();
}
private String getParsedMyXML() throws Exception {
StringBuffer inLine = new StringBuffer();
/* Get a SAXParser from the SAXPArserFactory. */
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
/* Get the XMLReader of the SAXParser we created. */
XMLReader xr = sp.getXMLReader();
/* Create a new ContentHandler and apply it to the XML-Reader */
XMLHandler myExampleHandler = new XMLHandler();
xr.setContentHandler(myExampleHandler);
/* Load xml file from raw folder*/
InputStream in = this.getResources().openRawResource(R.raw.a);
/* Begin parsing */
xr.parse(new InputSource(in));
XMLDataSet parsedExampleDataSet = myExampleHandler.getParsedData();
String s = parsedExampleDataSet.getDate();
String s2 = parsedExampleDataSet.getScore();
System.out.println("s is:::::: "+s);
System.out.println("s2 is :::::"+s2);
System.out.println(parsedExampleDataSet);
inLine.append(parsedExampleDataSet.toString());
in.close();
return inLine.toString();
}
}
package barc3.barc3;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class XMLHandler extends DefaultHandler{
// ===========================================================
// Fields
// ===========================================================
private boolean NewDataSet = false;
private boolean Table = false;
private boolean Date = false;
private boolean Score = false;
private XMLDataSet myParsedExampleDataSet = new XMLDataSet();
// ===========================================================
// Getter & Setter
// ===========================================================
public XMLDataSet getParsedData() {
return this.myParsedExampleDataSet;
}
// ===========================================================
// Methods
// ===========================================================
@Override
public void startDocument() throws SAXException {
this.myParsedExampleDataSet = new XMLDataSet();
}
@Override
public void endDocument() throws SAXException {
// Nothing to do
}
/** Gets be called on opening tags like:
* <tag>
* Can provide attribute(s), when xml was like:
* <tag attribute="attributeValue">*/
@Override
public void startElement(String namespaceURI, String localName,
String qName, Attributes atts) throws SAXException {
if (localName.equals("NewDataSet")) {
this.NewDataSet = true;
}else if (localName.equals("Table")) {
this.Table = true;
}else if (localName.equals("Date")) {
this.Date = true;
}
else if (localName.equals("Score")) {
this.Score = true;
}
}
/** Gets be called on closing tags like:
* </tag> */
@Override
public void endElement(String namespaceURI, String localName, String qName)
throws SAXException {
if (localName.equals("NewDataSet")) {
this.NewDataSet = false;
}else if (localName.equals("Table")) {
this.Table = false;
}else if (localName.equals("Date")) {
this.Date = false;
}
else if (localName.equals("Score")) {
this.Score = false;
}
}
/** Gets be called on the following structure:
* <tag>characters</tag> */
@Override
public void characters(char ch[], int start, int length) {
if(this.Date)
{
myParsedExampleDataSet.setDate(new String(ch, start, length));
System.out.println("Date"+Date);
}
if(this.Score)
{
myParsedExampleDataSet.setScore(new String(ch, start, length));
System.out.println("Score"+Score);
}
}
}
package barc3.barc3;
public class XMLDataSet {
String Date = null;
String Score = null;
public String getDate() {
return Date;
}
public void setDate(String Date) {
this.Date = Date;
}
public String getScore() {
return Score;
}
public void setScore(String Score) {
this.Score = Score;
}
public String toString(){
return "Date is = " + this.Date
+ "\nScore is = " + this.Score;
}
}
<?xml version="1.0" encoding="utf-8" ?>
<DataSet xmlns="http://tempuri.org/">
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
- <xs:complexType>
- <xs:choice minOccurs="0" maxOccurs="unbounded">
- <xs:element name="Table">
- <xs:complexType>
- <xs:sequence>
<xs:element name="Date" type="xs:string" minOccurs="0" />
<xs:element name="Score" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
<NewDataSet xmlns="">
<Table diffgr:id="Table1" msdata:rowOrder="0">
<Date>12/5/2011</Date>
<Score>5</Score>
</Table>
- <Table diffgr:id="Table2" msdata:rowOrder="1">
<Date>45/5/2011</Date>
<Score>54</Score>
</Table>
- <Table diffgr:id="Table3" msdata:rowOrder="2">
<Date>12/5/2011</Date>
<Score>12</Score>
</Table>
- <Table diffgr:id="Table4" msdata:rowOrder="3">
<Date>3/4/2011</Date>
<Score>25</Score>
</Table>
- <Table diffgr:id="Table5" msdata:rowOrder="4">
<Date>6/8/2011</Date>
<Score>45</Score>
</Table>
</NewDataSet>
</diffgr:diffgram>
</DataSet>
答案 0 :(得分:2)
问题出在您的DefaultHandler
- 班级:
myParsedExampleDataSet.setDate(new String(ch, start, length))
characters
- 方法可以在一个标记上多次调用,例如,如果此标记包含>
的实体>
。
您应该在此方法中使用StringBuilder,这样您就可以将所有新内容附加到其中,然后使用toString()
- 方法获取字符串。