Question

I am new to programming, so to start with correct me if I am wrong in the paragragh below :

There is mainly three xml parsers for use in Android : Sax, Dom, and XmlPullParser. That last option, while existing as an external ressource. Is "in the core" of Android, thus working faster, but the functionnalities are limited

Ok here is my Question I slightly modified the code provided in the link below

http://developer.android.com/reference/org/xmlpull/v1/XmlPullParser.html

I did the following :

import java.io.IOException;
import java.io.StringReader; 
import org.xmlpull.v1.XmlPullParser; 
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory; 

public class xmlPParser
{   

    public String texte;
    public xmlPParser (String arg)         
      throws XmlPullParserException, IOException     
    {
         XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
         factory.setNamespaceAware(true);
         XmlPullParser xpp = factory.newPullParser();
         xpp.setInput( new StringReader ( arg ) );
         int eventType = xpp.getEventType();



         while (eventType != XmlPullParser.END_DOCUMENT)
       {
            //if(eventType == XmlPullParser.START_DOCUMENT) { System.out.println("Start document");} 
       // else if(eventType == XmlPullParser.START_TAG) { System.out.println("Start tag "+xpp.getName()); } 
        //else if(eventType == XmlPullParser.END_TAG) {  System.out.println("End tag "+xpp.getName()); }
        if(eventType == XmlPullParser.TEXT){ texte = xpp.getText();  }    //{ System.out.println("Text "+xpp.getText());}          

        eventType = xpp.next();
            }         

         //System.out.println("End document");
         } 

        public String getTexte()
        {
            String returnTexte = texte;
            return returnTexte;
        }

} 

In another java file, I can call the parser in the following way :

public xmlPParser myxpp;

...

myxpp = new xmlPParser("<foo>Hi five !!</foo>");

On that last line : I would like to be able to ask the parser to go to a file, instead of passing a string to it. how would i do that ? I am not sure how to make use of this posting Does getResources().getXml() supposes I am using the Android pullParser which I am not sure to be using now ?

Was it helpful?

Solution

XmlPullParser is not really a parser, it is an interface to a type of parser, called a "pull" parser.

The function getResources().getXml() returns an implementation of XmlPullParser for "parsing" XML resources. This is not a real XML parser -- in fact the original XML file was parsed at build time before being built into your app, and what this "XML parser" is doing is just returning the pre-digested XML structure as you call its API. This is the fastest "XML" parser available on Android (because it is not really parsing anything), but requires that the XML document be compiled as part of building your app.

The other implementation of XmlPullParser that you get from XmlPullParserFactory.newInstance() is not "limited" -- this implementation is full-featured, and can parse any raw XML document you give to it.

At least at one time (not sure if this is still the case), both the SAX parser and the parser returned by XmlPullParserFactory.newInstance() are actually built on the same underlying implementation, which is expat. The expat parser is a "push" parser (that is the same model as SAX), so the most efficient way to use it is with the SAX API. The XmlPullParser version has some more overhead from SAX since it needs to turn the underlying push semantics into a pull interface.

If it helps -- push means that it pushes the parsed data to you (callbacks you implement giving you each tag and other document element), while pull means you make calls to the parser to retrieve each element.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top