Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SAX parser vs XMLPull parser

Tags:

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.

like image 836
Nelson Ramirez Avatar asked Apr 27 '11 17:04

Nelson Ramirez


People also ask

What advantages does a SAX parser have over a DOM parser?

1)SAX is faster than DOM. 2)SAX is good for large documents because it takes comparitively less memory than Dom. 3)SAX takes less time to read a document where as Dom takes more time. 4)With SAX we can access data but we can't modify data.

What is the advantage of SAX parser?

The general advantages of SAX include: The nature of a streaming model means that you need far less memory to process large XML documents. You do not have to process the entire document. Use callback procedures to identify and respond to only the XML elements you are interested in.

Why is SAX parser faster than Dom?

SAX is faster than DOM (usually felt when reading large XML document) because SAX gives you information as a sequence of events (usually accessed through a handler) while DOM creates Nodes and manages the node creation structure until a DOM tree is fully created (as represented in the XML document).


2 Answers

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)

like image 153
Robert Massaioli Avatar answered Nov 08 '22 18:11

Robert Massaioli


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.

like image 21
varun bhardwaj Avatar answered Nov 08 '22 19:11

varun bhardwaj