SAX解析器中的问题

时间:2011-05-25 12:02:41

标签: android

  

我需要从xml文件中获取信息       我的问题是我无法从xml文件中获得正确的响应

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>

1 个答案:

答案 0 :(得分:1)

我认为错误在于URL url = new URL("C://Users//nik//Desktop//a.xml");

您将在模拟器或设备中运行程序,设备或模拟器中没有此类路径。您可以将xml文件放在资源中并从那里读取文件。

如果我的回复对您有帮助,不要忘记投票。

由于