Monday, 21 April 2014

DOM  Parser:-

The Document Object Model (DOM) is a platform and language-independent standard object model for representing XML and related formats. DOM is standard API which is not specific to any programming language. DOM represents an XML document as a tree model. The tree model makes the XML document hierarchal by nature. Each and every construct of the XML document is represented as a node in the tree.

SAX  Parser:-

SAX-Simple API for XML processing. SAX provides a mechanism for reading data from an XML document. It is a popular alternative to the Document Object Model (DOM).SAX provides an event based processing approach unlike DOM which is tree based.

Difference between SAX parser and DOM parser:-

SAX
DOM
A SAX parser takes the occurrences of components of an input document as events (i.e., event based processing), and tells the client what it reads as it reads through the input document.
A DOM parser creates a tree structure in memory from an input document and then waits for requests from client.
No navigation possible (top to bottom only once)
Whereas, we can navigate the DOM tree in any direction, any no. of times.
We cannot modify the document content in SAX
We can modify the document content in DOM
A SAX parser serves the client application always only with pieces of the document at any given time.
A DOM parser always serves the client application with the entire document no matter how much is actually needed by the client.
A SAX parser, however, is much more space efficient in case of a big input document
A DOM parser is space inefficient when the document is huge.
Use SAX parser when
  • Input document is too big for available memory.
  • When only a part of the document is to be read and we create the data structures of our own.
  • If you use SAX, you are using much less memory and performing much less dynamic memory allocation.
Use DOM when
  • Your application has to access various parts of the document and using your own structure is just as complicated as the DOM tree.
  • Your application has to change the tree very frequently and data has to be stored for a significant amount of time.

0 comments:

Post a Comment

Subscribe to RSS Feed Follow me on Twitter!