android – SAX parser vs XMLPull parser-ThrowExceptions

Exception or error:

I understand the difference between how the SAX parser works vs the XMLPull parser.
In fact there’s a pretty good explanation here:
The article is a bit .NET centric but the concepts apply.

While I agree with the author’s opinion that the Pull parser is easier to work with, I’m pretty confused as to which type of parser would be better in which situations.
If anyone could shed any light and point me to some more reading I would appreciate it.

Thank you.

How to solve:

I find that they both suck. (And I have a better solution to suggest)

You should use the Simple annotation based XML library. I love it and use it for all of my projects. If you read through the tutorial then I think you will find that it will be able to do everything that you want and much faster and with less code. (Thus being less bug prone) Internally the library uses those parsers that you were asking about to do the heavy lifting.

You can then read my blog post on including it in an Android project if you want. (It will work in every version of Android from atleast 1.5 up which means for everybody basically)


It totally depends on the situation for e.g If the xml file is really large than you can’t opt for DOM parsers as they will first bring the file in to memory and then it will be parsed and i found that parsing a file of size n requires 7n memory space. In this case you should opt for SAX parser its light and will consume less memory.

Second case is when the file is not really large, in this case you can go for XML pull parser because in this you will have full control on the xml you can skip the parsing cycle any where that is not possible in SAX. So if the tag you are looking for is the first one in the file then why would you go for whole file.

So as far as i know if you consider only speed with small file go with XML pull parser and If the file is large and you want to parse it all then go with SAX.


Both the parsers are basically the same memory/time wise.
The only thing being that with pull parser you can pull out the events like startelement and endelement and only heed the ones that you want to.

where as with android sax parsers, you have no choice, you just put code where you want to but you have to include all the events.

here is a link which you can refer for further reading.


I find the SAX model easier to work with in one specific situation: where you are going to build your own in-memory representation of the entire document (or at least major portions of it) with custom data structures. (If you aren’t particular about the data structure, then the DOM parser already does this.)


Pull and Sax are similar in the way that they are both low-level streaming approaches which are faster and more memory efficient than DOM, but pull has a few advantages over SAX:

Pull is easier to implement than SAX because you don’t have to maintain the state of your parser (using additional variables to be able to know in which place your parser currently is in the XML tree). The nested loops in your pull parser code will more or less match the XML hierarchy of your document so I think the Pull parser code is also more readable than the SAX parser code.

With pull parser code you can skip entire blocks that you don’t want to parse so it’s also more efficient than SAX which always extracts the main information of all nodes. Using a pull parser, you can also stop the parsing at any moment if you fetched the information you wanted, which is not possible with SAX.

Also, you can implement a SAX parser using a pull parser. The opposite is not possible.

For all these reasons I believe the pull parser is superior to SAX in all situations, however like SAX it’s not trivial to implement properly and you have to be careful. If you don’t need the low-level speed benefits of pull and SAX and your XML is clean, you can always use a higher-level parsing library like Simple to do the hard work for you.


I found better and more efficient output while using SAX rather than XMLPullParser… My scenario is to parse the attributes under a XML tag, i could do it easily and insert it into Database smoothly… I think it depends on situations, when i need to write on a XML file i prefer DOM Parser…

public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {
        currentElement = true;
        db = new DatabaseHelper(thecontext);
        if (qName.equals("Asa.Amms.Data.Entity.User")) {
            int length = attributes.getLength();
            for (int i = 0; i < length; i++) {
                String name = attributes.getQName(i);
                if (name.equals("Id")) {
                    id = Integer.parseInt(attributes.getValue(i));
                if (name.equals("Login")) {
                    LoginID = attributes.getValue(i).toString();
                if (name.equals("Name")) {
                    Name = attributes.getValue(i).toString();
                if (name.equals("Password")) {
                    Password = attributes.getValue(i).toString();
                if (name.equals("ProgramOfficerId")) {
                    user_ProgramOfficerId = Integer.parseInt(attributes.getValue(i).toString());
            Log.i("Baal dhukbe", id + LoginID + Name + Password);

            db.insertUser(id, LoginID, Name, Password, user_ProgramOfficerId);


i’d recommend using the XmlPullParser one.. the Sax parser didn’t retrieve the tag from a feed in my test.. xmlpullparser did that easily =)
also depends on your preferences also

Leave a Reply

Your email address will not be published. Required fields are marked *