wood burning stoves 2.0*
The moose likes XML and Related Technologies and the fly likes DOM and SAX Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "DOM and SAX" Watch "DOM and SAX" New topic
Author

DOM and SAX

Niranjan Prasad
Ranch Hand

Joined: Aug 13, 2005
Posts: 62
Can anybody tell when to go for SAX and DOM?

I know that even SAX is preffered to when reading Long XML docs.
Others that SAX is also prefered when data is not in XML format
Can anybody tell me abt no-XML format.


Niranjan
JigaR Parekh
Ranch Hand

Joined: May 23, 2005
Posts: 112
Use SAX parser when
- Input document is too big for available memory.
- When only a part of the document is to be read and we create the data structures of our own.
- If you use SAX, you are using much less memory and performing much less dynamic memory allocation.

Use DOM when
- Your application has to access various parts of the document and using your own structure is just as complicated as the DOM tree.
- Your application has to change the tree very frequently and data has to be stored for a significant amount of time.
Jeroen T Wenting
Ranch Hand

Joined: Apr 21, 2006
Posts: 1847
basically SAX is strictly read-only so if you need to modify the XML document in any form (or create one from scratch) you MUST use DOM.

Then there's the potential problem of repeated access to elements which is impossible in SAX, so if you need to do things which require you to process something more than once you will again need DOM (or use your own caching mechanism on top of SAX).

Then there's personal preference. I just like DOM better, to me it's more intuitive and easier to use than is SAX.


42
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12759
    
    5
basically SAX is strictly read-only so if you need to modify the XML document in any form (or create one from scratch) you MUST use DOM.

I can't agree with that. You can read using SAX, perform logical operations and write a new document using the data in SAX events. Naturally you are limited in the kinds of logic you can perform due to the streaming order of the SAX events.
For example, modifying the attributes in selected XML elements is easily done in a startElement method.
There are many other ways to create an XML document besides building a DOM in memory.
Bill
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: DOM and SAX
 
Similar Threads
XML
Difference Between DOM and SAX
What is the diff between SAX and DOM ?
SAX Dom parser
SAX vs DOM