my dog learned polymorphism
The moose likes XML and Related Technologies and the fly likes DOM or 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 or SAX" Watch "DOM or SAX" New topic


Maddy Tillie

Joined: Jan 10, 2002
Posts: 5
HI Guys,
Well i am new to XML and i have a query related to SAX and DOM. i just wanna know which one is best to use under which circumstances and what are the advantages and disadvantages apart from memory constraints. and if i am not wrong then these are not API's, these are just the framework and API's are provided by diff vendors like SUN. Correct me if i am wrong.
Thanks in advance
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
I use JDom ( It's an easy to use and yet powerful DOM for Java.
Jeff Chi

Joined: Jan 15, 2002
Posts: 6
Which API do most firms use? DOM, JDOM, or SAX?
YanNaing WTint
Ranch Hand

Joined: Oct 30, 2001
Posts: 40
- event driven, a bit harder to visualise.
- a lot more programming then DOM
- fastest and lease memory-intensive.
- can't back up to earlier part of document because it is serial data stream.
- tree structure with nodes.
- object base
- allow create nodes, remove nodes, change their contents, and traverse the node hierarchy.

Yan Naing Wynn Tint (SCJP2, SCWCD)
Ajith Kallambella

Joined: Mar 17, 2000
Posts: 5782
I don't mean to rain the parade, but the names Maddy and Jeff c does not comply with the JavaRanch naming policy. We require names to have at least two words, separated by a space, and strongly recommend that you use your full real name. Please spare a moment and re-register with a name that meets these requirements.

Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Scott Bain
Ranch Hand

Joined: Dec 21, 2001
Posts: 46
SAX is extremely lightweight and fast. It does have limitations:
It's a reader only (you cannot write XML with SAX).
It's purely sequential. No random access to your data, just take it in document order.
No context. When the SAX event for a given node fires, no information about what node it is nested in is included... you have to track that yourself if it's important to you.
DOM is more complete -- it represents your entire data structure as a tree of nodes which can be "walked" in any way that suits you. Ask a node about its parents, siblings, children, attriburtes, through method calls on the object that represents it. Much more flexible and powerful.
DOM, however, requires that the entire tree be instantiated into memory, which can be a problem for large documents. Also, it's a fairly complex set of objects you're dealing with, with its own collections, etc... Finally, it's meant to be implemented in "any" language, so it does not take advantage of things like method overloading and the like.
For Java-specific development, take a look at JDom ( It's lighter-weight, uses Java collections (List), method overloading, etc... can be used creationally, and is very fast.
Brett McLaughlin, the author of the excellent "Java and XML", is one of the authors of JDom.

Scott Bain<br />Senior Consultant<br />Net Objectives<br />425-591-5844<br /><a href="" target="_blank" rel="nofollow">Net Objectives</a><br />----------------------------<br />* Sign up for our free newsletter by sending an e-mail to<br /><br />* Learn about and join our design pattern community of practice by going to<br /><a href="" target="_blank" rel="nofollow"></a><br />* Alan Shalloway & Jim Trott's - Design Patterns Explained: A New Perspective on<br />Object-Oriented Design is now available<br />* Our new CDROM-based XML training is now available as well
Rick Salsa
Ranch Hand

Joined: Jul 17, 2001
Posts: 173
How does something like Castor compare to SAX and DOM? I know the way it works is entirely different, but how come its concept isn't in wide use yet? I haven't used JDOM yet, is there something that would make it better than Castor, or JAXB for that matter? From what I've seen, Castor seems like the way to go for now...
Maddy Tillie

Joined: Jan 10, 2002
Posts: 5
Thanks guys for the help and i will re register too
Maddy tillie
I agree. Here's the link:
subject: DOM or SAX
It's not a secret anymore!