Alex Rivera | Logout

Parsing local XML file using Sax in Android

Asked 2010-04-28T09:06:09.103
12

Can anyone tell me how to parse a local XML file stored in the system using SAX, with an example code? Please also tell me where can I find information on that.

Edit
Report

2 Answers

19

To read from XML in your app, create a folder in your res folder inside your project called "xml" (lower case). Store your xml in this newly created folder. To load the XML from your resources,

XmlResourceParser myxml = mContext.getResources().getXml(R.xml.MyXml);//MyXml.xml is name of our xml in newly created xml folder, mContext is the current context
// Alternatively use: XmlResourceParser myxml = getContext().getResources().getXml(R.xml.MyXml);

myxml.next();//Get next parse event
int eventType = myxml.getEventType(); //Get current xml event i.e., START_DOCUMENT etc.

You can then start to process nodes, attributes etc and text contained within by casing the event type, once processed call myxml.next() to get the next event, i.e.,

 String NodeValue;
    while (eventType != XmlPullParser.END_DOCUMENT)  //Keep going until end of xml document
    {  
        if(eventType == XmlPullParser.START_DOCUMENT)   
        {     
            //Start of XML, can check this with myxml.getName() in Log, see if your xml has read successfully
        }    
        else if(eventType == XmlPullParser.START_TAG)   
        {     
            NodeValue = myxml.getName();//Start of a Node
            if (NodeValue.equalsIgnoreCase("FirstNodeNameType"))
            {
                    // use myxml.getAttributeValue(x); where x is the number
                    // of the attribute whose data you want to use for this node

            }

            if (NodeValue.equalsIgnoreCase("SecondNodeNameType"))
            {
                    // use myxml.getAttributeValue(x); where x is the number
                    // of the attribute whose data you want to use for this node

            } 
           //etc for each node name
       }   
        else if(eventType == XmlPullParser.END_TAG)   
        {     
            //End of document
        }    
        else if(eventType == XmlPullParser.TEXT)   
        {    
            //Any XML t
answered 2011-01-06T19:58:17.973
1

sample code

  1. Create documentBuilderFactory

    DocumentBuilderFactory factory= DocumentBuilderFactory.newInstance();

    1. Create DocumentBuilder

    DocumentBuilder builder=factory. newDocumentBuilder();

    1. get input stream ClassLoader cls=DomReader.class.getClassLoader(); InputStream is=cls.getResourceAsStream("xml file");
      1. parse xml file and get Document object by calling parse method on DocumentBuilder object. Document document=builder.parse(is);
      2. Traverse dom tree using document object. SAX: Simple xml parsing. It parses node by node Traversing is from top to bottom Low memory usage Back navigation is not possible with sax.

    //implementing required handlers public class SaxParse extends DefaultHandler{ } //new instance of saxParserFactory SAXParserFactory factory=SAXParserFactory.newInstance(); //NEW INSTANCE OF SAX PARSER SAXParser saxparser=factory.newSAXParser(); //Parsing xml document SAXParser.parse(new File(file to be parsed), new SAXXMLParserImpl());

answered 2012-11-07T09:31:05.383

Your Answer