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

difference between SAX and DOM?

gopal kishan
Ranch Hand

Joined: Feb 23, 2005
Posts: 99
Hi All,

can any one tell me the difference between sax and dom parser??

and which is best??

thanks in advance
Gopal
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24185
    
  34

A DOM parser builds a tree of nodes that represent the XML nodes, elements and attributes that comprise a document. It's convenient and intuitive to use, but memory intensive and slow.

A SAX parser is "event driven." You write a handler that is notified when the parser sees an opening or closing element, an attribute, etc. You then do whatever you want with that information. It can be hard for some people to understand, but it's fast and memory-efficient.


[Jess in Action][AskingGoodQuestions]
gopal kishan
Ranch Hand

Joined: Feb 23, 2005
Posts: 99
hi Friedman-Hill,

Thanks for your reply.

I know that SAX is a one way parser, and DOM is a two way parser. it means SAX can parse XML to java object but not from java object to XML, but DOM does two ways.

is it right?? please clarify.....


thanks in advance
Gopal
Rajagopal Manohar
Ranch Hand

Joined: Nov 26, 2004
Posts: 183
You cannot modify an XML document file using SAX you can only read it.
You cannot create a new XML document from scratch using SAX.

If you want to do either of these then use DOM or even better JDOM

Regards,
Rajagopal
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12789
    
    5
You cannot modify an XML document file using SAX you can only read it.
You cannot create a new XML document from scratch using SAX.

Close but not exactly true -
You can use SAX to modify an input XML document if you write the modified document on the fly. Since you only get access to one element at a time, you are limited as to the types of modifications BUT there is no limit on document size the way DOM limits you to a document that fits in memory.
The Cocoon project does some amazing things with "pipelines" of SAX events.
Bill
 
Don't get me started about those stupid light bulbs.
 
subject: difference between SAX and DOM?