aspose file tools*
The moose likes XML and Related Technologies and the fly likes SAX vs 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 "SAX vs DOM" Watch "SAX vs DOM" New topic
Author

SAX vs DOM

Mortin kim
Ranch Hand

Joined: Nov 15, 2007
Posts: 44
can any one tell me what are the differences between SAX and DOM..?
Where Excetly DOM Fits and SAX fits.?
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Originally posted by Mortin kim:
can any one tell me what are the differences between SAX and DOM..?
Where Excetly DOM Fits and SAX fits.?


In very brief, SAX is lowest level parser works on event based mechanism. DOM instead loads XML document into memory as a tree and then traverse node for information.

If XML is small then SAX is good else DOM.

Usually people don't use these low level parsers nowadays. You can look for JDOM, Castor, XMLBeans, JAXB etc.

Search forum or net for more information.

William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12825
    
    5
If XML is small then SAX is good else DOM.


That is exactly backwards from the usual advice (which is not all that useful anyway.)
People decide between SAX and DOM by looking at what the program has to do with the document once it has been read.

Bill
Mortin kim
Ranch Hand

Joined: Nov 15, 2007
Posts: 44
HI rathi ji.,
what is event based machanisam..can you explain brifly..?
Masoud Kalali
Author
Ranch Hand

Joined: Jul 08, 2004
Posts: 531

Originally posted by Mortin kim:
can any one tell me what are the differences between SAX and DOM..?
Where Excetly DOM Fits and SAX fits.?


You may use DOM when you are going to build xml from elements or you need to traverse between xml nodes backward and forward to decide on your next action, edit the document...
Dom will load your xml into memory so it is memory consumer, from the other hand it gives you the most possible flexibility with an almost easy to use API.


SAX, on the other hand, will pars the document and let you catch some events based on what kind of element parser found during its parse operation. It is forward only, fast and it is not that kind of memory consumer which DOM is. It can not help you with XML authoring and client (your code which use the parser) will bound to parser to complete its operation.

FYI: Stax, let you have all good thing that sax has, also it allows you to write xml, it is not a pull parse so your parser client will not lock for the parser until it complete the document.

You should select your Parser based on several condition like:
-maximum and minimum size of document which need to be parsed
-validation requirement
-one way parsing or traversing in the document
-...

hope it helps.


Masoud Kalali
Software Engineer - My Weblog - GlassFish Security
Daesung Park
Ranch Hand

Joined: Mar 22, 2007
Posts: 68
I can say it simply.
DOM: I am interested in hierarchy of elements. That means I want to manipulate easily elements of different depth within a method.
SAX: I want to use DOM, but I don't have enough memory and XML files are too big.


Daesung Park

BLOG
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Originally posted by William Brogden:

:shocked:
That is exactly backwards from the usual advice (which is not all that useful anyway.)
People decide between SAX and DOM by looking at what the program has to do with the document once it has been read.

Bill


Sorry. My mistake. It's just reverse.

Mortin, XML has some character which has meaning like <, >, " etc. Events are raised whenever such character occurs and interpreted accordingly.

Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

Move away from SAX and DOM , use StAX.


Groovy
Dilshan Edirisuriya
Ranch Hand

Joined: Apr 22, 2006
Posts: 299
Prad, what are the advantages you can get by using STAX? How do you supposed to replace that SAX/ DOM behavior by STAX?


Dilshan Edirisuriya SCJP1.4, SCWCD1.4, SCBCD 5
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

I would like to correct myself. Use Stax whereever possible.I can't be used everywhere.Stax has the disadvantages that it is not easier to navigate like DOM. No ability to create,update, delete elements


Here is a good article comparing the three approaches
http://www.oracle.com/technology/oramag/oracle/03-sep/o53devxml.html

http://java.sun.com/javaee/5/docs/tutorial/doc/bnbdw.html#bnbea
Dilshan Edirisuriya
Ranch Hand

Joined: Apr 22, 2006
Posts: 299
Yes Prad that links describes those issues. Thank you
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: SAX vs DOM