Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is the diff between SAX and DOM ?

 
Tejas Patel
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Friends !!!
Can u please tell me what is the diff betn. SAX and DOM ?
and when to use SAX and When to Use DOM ?
Anyone tell me?
Thanx.
Tejas.
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
SAX is based on 'events' ( aka call-backs ). It is simple, sequential way of looking at the XML data. Advantages of using SAX are it is lightweight in resource consumption and it is quite fast too. The downside of it is that it is a 'read only' mechanism ie., you will not be able to modify the data. Since it is sequential, you will not have access to past elements or peek ahead and see what is next. No random access either.
DOM on the otherhand reads the entire XML document once and creates a 'tree-like' structure in the memory. You can seek 'nodes' back and forth and traverse the XML tree in any random order. Unlike SAX you can modify the XML data and save it back which makes it a better choice when you need to change the XML data during parsing. You can also peek ahead or go back to any node. The downside of DOM is that since the entire XML is stored in the memory and manipulated, it is memory intensive.
Whether to use SAX or DOM is more of a design choice and should be driven by what you want to achieve. For example, if you need flexibility of being able to manipulate different elements and traverse different nodes in a non-sequential manner, DOM is the obvious choice. But DOM can be a pain if your XML file is large. I have seen some implementations where SAX is used with some additional programming to achive the flexibility of DOM while not compromising on performance.
Hope that helps!
Ajith
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here are some more differentiating features -

  • SAX is developed by XML-Dev mailing list and DOM is a W3C recommendation.
  • DOM spec is written in CORBA IDL, SAX spec is written in java.
  • DOM spec is 500 pages whereas the SAX spec is only 20 pages.
  • SAX gives control to user during parsing, DOM gives control only after parse.
  • DOM provides range support, traversal support, HTML DOM support and CSS/Stylesheet support. SAX lacks these things.

  • Ajith
 
Murali Mohan
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
5)We can stop the SAX parsing when ever and where ever you want.
6)SAX is sequential parsing but with DOM we can move to back also.
7)To parse machine generated code SAX is better.To parse human readable documents DOM is useful.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic