Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The difference among : SAX Parser,XPath,DOM,XMLPullParser

I want to know the difference between the four above types (SAXPaser, XPath, DOM, XMLPullParse) and when should we use each one.

like image 294
tree hh Avatar asked May 26 '12 10:05

tree hh


People also ask

What is the difference between SAX and DOM based XML processing?

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 are the different types of parser used in XML?

In PHP there are two major types of XML parsers: Tree-Based Parsers. Event-Based Parsers.

Why is SAX parser faster than DOM?

A SAX parser, however, is much more space efficient in case of a big input document (because it creates no internal structure). What's more, it runs faster and is easier to learn than DOM parser because its API is really simple.


3 Answers

SAX Parsing is the Best one to implement than DOM, see the difference between these two in the following:

DOM

The Nodes are in the form of Tree Structure Memory: It Occupies more memory, DOM is only preffered in the case of small XML documents Slower at runtime Stored as an objects Programmatically easy to implement Ease of navigation and use.

SAX

Sequence of events It doesn't use any memory preferred for large documents. Faster at runtime, because of the above mentioned point. Objects are to be created. Need to write code for creating objects In SAX Backward navigation is not possible as it sequentially processes the document

So if you have very large files then you should use SAX parser since it will fire events and releasing them ,nothing is stored in memory ,and using SAX parser you can't access element in a random way there is no going back ! , but Dom let you access any part of the xml file since it keeps the whole file/document in memory .

see this article and you can get what you want by reading the Summary.

also check this link to view performance of different xml parser

enter image description here

like image 81
K_Anas Avatar answered Oct 10 '22 08:10

K_Anas


Please check below links...

http://steveliles.github.com/comparing_methods_of_xml_parsing_in_android.html

http://xjaphx.wordpress.com/2011/11/01/android-xml-adventure-compare-xml-parsers/

http://www.ibm.com/developerworks/opensource/library/x-android/index.html

http://www.developer.com/ws/android/development-tools/Android-XML-Parser-Performance-3824221-2.htm

http://www.geekinterview.com/question_details/12797

(As Per above Article)

Both SAX and DOM are used to parse the XML document. Both has advantages and disadvantages and can be used in our programming depending on the situation

SAX:

  1. Parses node by node
  2. Doesnt store the XML in memory
  3. We cant insert or delete a node
  4. Top to bottom traversing

DOM

  1. Stores the entire XML document into memory before processing
  2. Occupies more memory
  3. We can insert or delete nodes
  4. Traverse in any direction.

If we need to find a node and doesnt need to insert or delete we can go with SAX itself otherwise DOM provided we have more memory.

like image 31
SBJ Avatar answered Oct 10 '22 09:10

SBJ


DOM

The Nodes are in the form of Tree Structure Memory: It Occupies more memory, DOM is only preffered in the case of small XML documents..Store the entire XML document into memory befor processing Slower at runtime Stored as an objects Programmatically easy to implement Ease of navigation and use,can traverse in any direction. We can insert or delete,alter nodes.

SAX : use when you want to access XML ( not alter XML)

Sequence of events It doesn't use any memory preferred for large documents.Doesn't store the XML in memory before processing Faster at runtime, because of the above mentioned point. Objects are to be created. Need to write code for creating objects In SAX Backward navigation is not possible as it sequentially processes the document,top to bottom traversing We can't insert or delete a node

XPATH: Xpath is useful when you only need a couple of values from the XML document and you know where to find them(you know the path of the data./root/item/challange/text)

XMLPullParser: Fast and requires less memory with DOM

Source: http://www.time2ask.com/ http://www.time2ask.com/Android/The-difference-among-SAX-ParserXPathDOMXMLPullParser/_2361836

like image 20
suresh Avatar answered Oct 10 '22 08:10

suresh