• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

difference between SAX and DOM?

 
gopal kishan
Ranch Hand
Posts: 99
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
gopal kishan
Ranch Hand
Posts: 99
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 183
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13045
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic