Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

SAX vs DOM

 
Mortin kim
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can any one tell me what are the differences between SAX and DOM..?
Where Excetly DOM Fits and SAX fits.?
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13064
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI rathi ji.,
what is event based machanisam..can you explain brifly..?
 
Masoud Kalali
Author
Ranch Hand
Posts: 531
Java Mac OS X Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Daesung Park
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Move away from SAX and DOM , use StAX.
 
Dilshan Edirisuriya
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prad, what are the advantages you can get by using STAX? How do you supposed to replace that SAX/ DOM behavior by STAX?
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Prad that links describes those issues. Thank you
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic