GeeCON Prague 2014*
The moose likes XML and Related Technologies and the fly likes What is the diff between SAX and DOM ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "What is the diff between SAX and DOM ?" Watch "What is the diff between SAX and DOM ?" New topic
Author

What is the diff between SAX and DOM ?

Tejas Patel
Greenhorn

Joined: Oct 03, 2000
Posts: 11
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

Joined: Mar 17, 2000
Posts: 5782
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


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
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

Joined: Jun 14, 2001
Posts: 47
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.
 
GeeCON Prague 2014
 
subject: What is the diff between SAX and DOM ?