-3
我需要從xml文件中獲取信息。
我的問題是我不能讓從XML文件正確的響應SAX解析器中的問題
package com.xmlparser;
import java.net.URL;
import java.util.ArrayList;
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.os.Bundle;
import android.widget.TextView;
public class xmlparser extends Activity {
/** Called when the activity is first created. */
detaset dt=null;
detaset Date;
ArrayList<String> Score;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try
{
URL url = new URL("C://Users//nik//Desktop//a.xml");
System.out.println(url);
TextView tv = new TextView(this);
/* 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*/
Handler myExampleHandler = new Handler();
xr.setContentHandler(myExampleHandler);
/* Parse the xml-data from our URL. */
xr.parse(new InputSource(url.openStream()));
/* Parsing has finished. */
/* Our ExampleHandler now provides the parsed data to us. */
detaset parsedExampleDataSet = myExampleHandler.getParsedData();
/* Set the result to be displayed in our GUI. */
System.out.println(parsedExampleDataSet.toString());
// Date = myExampleHandler.getParsedData();
//System.out.println(Date);
this.setContentView(tv);
}catch (Exception e) {
e.printStackTrace();
}
}
// TODO: handle exception
/* Display the TextView. */
}
package com.xmlparser;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class Handler extends DefaultHandler{
// ===========================================================
// Fields
// ===========================================================
private boolean NewDataSet = false;
private boolean Table = false;
private boolean Date = false;
private boolean Score = false;
private detaset myParsedExampleDataSet = new detaset();
// ===========================================================
// Getter & Setter
// ===========================================================
public detaset getParsedData() {
return this.myParsedExampleDataSet;
}
// ===========================================================
// Methods
// ===========================================================
@Override
public void startDocument() throws SAXException {
this.myParsedExampleDataSet = new detaset();
}
@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")) {
// Extract an Attribute
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;
System.out.println("Newdataset"+NewDataSet);
}else if (localName.equals("Table")) {
this.Table = false;
System.out.println("Table"+Table);
}else if (localName.equals("Date")) {
this.Date = false;
}else if (localName.equals("Score")) {
// Nothing to do here
this.Score = false;
System.out.println("Score"+Score);
}
}
/** 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));
}
if(this.Score){
myParsedExampleDataSet.setScore(new String(ch, start, length));
}
}
}
package com.xmlparser;
public class detaset {
private String Date = null;
private String Score = null;
public void setDate(String Date) {
this.Date = Date;
}
public String getDate()
{
return Date;
}
public void setScore(String Score) {
this.Score = Score;
}
public String getScore()
{
return 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>
</NewDataSet>
</diffgr:diffgram>
</DataSet>
那麼,這個「問題」有什麼意義? – Stephan 2011-05-25 12:24:34
問題或問題是什麼? – Hasandroid 2011-05-25 12:24:55
這不是問題。代碼太多,甚至沒有提示問題是什麼。 – 2011-05-25 12:44:40