This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
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

JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "DOM and SAX" Watch "DOM and SAX" New topic


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.

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.

William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13001
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.
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: DOM and SAX
It's not a secret anymore!