Saturday, 9 February 2013

SAX vs DOM

Java the XML parser contain two types of it,  SAX and DOM ,

SAX is evenet based and DOM is tree model. 


Let me explain it: 

SAX :events are triggered when the XML is being parsed.

Like when we click particular node then it will give all the sub nodes rather than loading all the nodes at the same time. 

When the parser is parsing the XML, and encounters a tag starting (e.g<something>), 
it triggers the 'tagStared' event (actual name of event might differ). 

Similarly when the end of the tag is met while parsing (</something>), it triggerstagEnded

Using a SAX parser implies you:

  1. need to handle these events 
  2. make sense of the data returned with each event.




DOM parse : it will load all the nodes and makes the tree model and there are no events triggered while parsing. 

The entire XML is parsed and a DOM tree (of the nodes in the XML) is generated and returned. 

Once parsed, the user can navigate the tree to access the various data previously embedded in the various nodes in the XML.


In general, DOM is easier to use but has an overhead of parsing the entire XML before you can start using it.

No comments:

Post a Comment