Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference among XML SAX parser, Pull parser & DOM Parser in android

I want to know what's the difference between XML SAX parser, Pull parser & DOM parser in Android. In which condition, which one is better in performance and implementation?

Thanx. Khobaib.

like image 494
Khobaib Avatar asked Jul 02 '12 16:07

Khobaib


People also ask

What is the difference between SAX parser and DOM parser?

DOM stands for Document Object Model while SAX stands for Simple API for XML parsing. DOM parser load full XML file in-memory and creates a tree representation of XML document, while SAX is an event based XML parser and doesn't load whole XML document into memory.

What is pull parser?

Streaming pull parsing refers to a programming model in which a client application calls methods on an XML parsing library when it needs to interact with an XML infoset; that is, the client only gets (pulls) XML data when it explicitly asks for it.


1 Answers

Dom Parser - It uses object based approach. i.e., it first loads the entire XML in memory, converts the XML nodes into objects and then starts parsing them. So, it is pretty slower.

SAX and PULL Parser - they use event based approach. Both are almost same in terms of memory and performance. However there are few distinguishing situations on when to use them as described below.

Dom Parser - Use it when you need to validate the entire XML before parsing. Never use it when the XML is too large and validation can be compromised. Once it starts parsing, it parses from starting node to ending node. there is no way to parse only particular nodes.

SAX - Use it when you want to parse the entire XML. Once it starts parsing, it parses from starting node to ending node. there is no way to parse only particular nodes.

PULL - Use it when you don't want to parse the entire XML. It is easier to implement than SAX, because you don't have to maintain the state of your parser. You can pull only a particular node you are interested at.

My suggestion is - Need validation - Go for DOM, File size is small or you are not interested in parsing of entire XML- Go for PULL, File size is large or you need to parse the entire XML file - Go for SAX.

like image 99
Shivanand Darur Avatar answered Sep 27 '22 23:09

Shivanand Darur