SAX parser vs XMLPull parser
Asked Answered
S

7

22

I understand the difference between how the SAX parser works vs the XMLPull parser. In fact there's a pretty good explanation here:

http://www.firstobject.com/xml-reader-sax-vs-xml-pull-parser.htm 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.

Sniper answered 27/4, 2011 at 17:27 Comment(0)
A
18

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)

Angelineangelique answered 28/4, 2011 at 0:52 Comment(5)
Thanks Robert! I read through it and really like it as well. I'll save my comments on it after I play with it some moreSniper
@Robert WTF Robert :-) Your site seems down!Stocking
@dbm: I'm very sorry about that. I had to move my blog from one domain to another and there are still erroneous links lying around the place and this was one of them. I have edited this post and put in the link to where the blog moved to. Thankyou for alerting me and I hope that people now enjoy the new posts. (I am disappointed because it was supposed to still be throwing a 301 redirect at the old host).Angelineangelique
SimpleXML isn't suitable for large XMLs though. Go SAX for that.Philosophism
This is android, so you are constrained with memory and CPU. So these are real concerns: Does it slurp the whole xml? Is the code efficient?Grays
B
11

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.

Buttock answered 14/7, 2011 at 13:9 Comment(3)
well, yesterday it was acting up, maybe it was a server side thingPhototransistor
Sry guys for late reply but yeah this link is broken. I will get back to you asap with a better answer.Buttock
The link was broken so i wrote my understanding, please update if something is missingButtock
S
3

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.

Sabra answered 27/4, 2011 at 17:43 Comment(0)
X
2

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.)

Xochitlxp answered 27/4, 2011 at 17:40 Comment(0)
N
2

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.

Nipping answered 3/5, 2014 at 13:15 Comment(0)
B
0

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);
        }
}
Bandicoot answered 14/6, 2015 at 5:49 Comment(0)
W
-2

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

Wouldbe answered 4/5, 2011 at 20:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.