File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes Difference between SAX and DOM?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Difference between SAX and DOM??" Watch "Difference between SAX and DOM??" New topic
Author

Difference between SAX and DOM??

gopal kishan
Ranch Hand

Joined: Feb 23, 2005
Posts: 99
HI All,
I read many arrticles about the difference between SAX and DOM.

I came to know that SAX is a oneway parser, it means it can parse from XML to java. Whereas DOM is a two way parser, it means it can parse from XML to java as well as java to XML .

Is it true??

please clarify

thanks in advance
Gopal
DW Bolton
Greenhorn

Joined: Jun 29, 2005
Posts: 28
To say SAX is one-way and DOM is two-way is sort of true but really it is totally missing the point.

The 'truth' is that SAX is an event based parser, whereas DOM is a tree model parser.

SAX events could trigger output of a new document easily enough, in which case you could call it two way, just as DOM could be used purely to read and not to manipulate, in which case you could call it one way.

Your last post and the responses covered this ground though, I think you've got to realign your thoughts on these parsing models to what your previous responders and I have said -- that is SAX = event driven, DOM = tree model.
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 806
hi gopal..
can u please give the links telling the difference b/w sax and dom ?

list various links..for usefull study..

thanx ..
do reply


Thanks and Regards, Amit Taneja
ram prabhu
Greenhorn

Joined: Aug 18, 2005
Posts: 8
Hai....can u explain it briefly about the event driven...
David Patterson
Ranch Hand

Joined: Jul 01, 2002
Posts: 65
The main interface involved in SAX is a ContentHandler. You write your own class that implments this interface. You supply methods to respond to events. One method is called when the document starts, another when the document ends. One is called when an element starts, one when it ends. Between these two there may be calls to a "characters" method if there are text character specified between the start end end tags. If elements are nested, you may get two starts then two ends.

The entire procesing is up to you. The sequence follows the input source. If you don't care about a specific element when it is processed, do nothing.

When the document end method is called, SAX is finished. Whatever you have kept in whatever format is all that is kept.

This is in contrast to DOM which reads the entire input and constructs a tree of elements. Then entire source is represented by the tree. You can move elements or attributes around to make a different file, you can run it through a transformer. You can search it using XPath to find sequences of elements or structures in the document and process them as you wish. When you are done, you can serialize it (to produce an XML file, or an xml-format stream.

So, SAX is a Simple API for XML as its name implies. It does not have large demands for memory. You can process a huge file and if you don't want to keep much data, or you are summing data from the elements that go by, you will not require much memory. DOM builds a tree of Nodes to represent the entire file. It takes more space to hold an element than it takes for the minimal character representation -- "<a/>" 4 characters vs. dozens or hundreds.

Both will process the same input, and with SAX, you will see all input as it goes by. You may keep what you want in whatever format you want. But, if you don't keep it, it is not stored somewhere for you to process unless you run the input source through SAX again.

Does this help you understand the differences?

Dave Patterson
sampath kumar yanagandla
Greenhorn

Joined: Apr 29, 2007
Posts: 13
I want to design a utility class to get values of elements and attributes of xml document which parser can i choose???
thanks in advance
Dilip Kumar Aerroju
Greenhorn

Joined: Oct 09, 2007
Posts: 3
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
• Parses node by node
• Doesn’t store the XML in memory
• We cant insert or delete a node
• SAX is an event based parser
• SAX is a Simple API for XML
• doesn’t preserve comments
• SAX generally runs a little faster than DOM

DOM
• Stores the entire XML document into memory before processing
• Occupies more memory
• We can insert or delete nodes
• Traverse in any direction.
• DOM is a tree model parser
• Document Object Model (DOM) API
• Preserves comments
• SAX generally runs a little faster than DOM

If we need to find a node and doesn’t need to insert or delete we can go with SAX itself otherwise DOM provided we have more memory.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Difference between SAX and DOM??
 
Similar Threads
Difference between DOM and SAX Parser
DOM or SAX confusing
difference between SAX and DOM?
what is SAX (event-based) parsing ?
Newbie Question: XML on Java