开发者

Problem in SAX parser

i need to get information from xml file.

my problem is i cant get proper response from xml file

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() thr开发者_如何学Goows 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>


I think the mistake is in the line URL url = new URL("C://Users//nik//Desktop//a.xml");

You will run the program in emulator or in device there is no such path in either device or emulator. You can put the xml file in assets and read the file from there.

Donot forget to vote if my response is helpful for you.

Thanks

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜