• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Difference between SAX and DOM??

 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI All,
I read many arrticles about the difference between SAX and DOM.

I came to know that SAX is a oneway parser, it means it can parse from XML to java. Whereas DOM is a two way parser, it means it can parse from XML to java as well as java to XML .

Is it true??

please clarify

thanks in advance
Gopal
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To say SAX is one-way and DOM is two-way is sort of true but really it is totally missing the point.

The 'truth' is that SAX is an event based parser, whereas DOM is a tree model parser.

SAX events could trigger output of a new document easily enough, in which case you could call it two way, just as DOM could be used purely to read and not to manipulate, in which case you could call it one way.

Your last post and the responses covered this ground though, I think you've got to realign your thoughts on these parsing models to what your previous responders and I have said -- that is SAX = event driven, DOM = tree model.
 
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi gopal..
can u please give the links telling the difference b/w sax and dom ?

list various links..for usefull study..

thanx ..
do reply
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hai....can u explain it briefly about the event driven...
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The main interface involved in SAX is a ContentHandler. You write your own class that implments this interface. You supply methods to respond to events. One method is called when the document starts, another when the document ends. One is called when an element starts, one when it ends. Between these two there may be calls to a "characters" method if there are text character specified between the start end end tags. If elements are nested, you may get two starts then two ends.

The entire procesing is up to you. The sequence follows the input source. If you don't care about a specific element when it is processed, do nothing.

When the document end method is called, SAX is finished. Whatever you have kept in whatever format is all that is kept.

This is in contrast to DOM which reads the entire input and constructs a tree of elements. Then entire source is represented by the tree. You can move elements or attributes around to make a different file, you can run it through a transformer. You can search it using XPath to find sequences of elements or structures in the document and process them as you wish. When you are done, you can serialize it (to produce an XML file, or an xml-format stream.

So, SAX is a Simple API for XML as its name implies. It does not have large demands for memory. You can process a huge file and if you don't want to keep much data, or you are summing data from the elements that go by, you will not require much memory. DOM builds a tree of Nodes to represent the entire file. It takes more space to hold an element than it takes for the minimal character representation -- "<a/>" 4 characters vs. dozens or hundreds.

Both will process the same input, and with SAX, you will see all input as it goes by. You may keep what you want in whatever format you want. But, if you don't keep it, it is not stored somewhere for you to process unless you run the input source through SAX again.

Does this help you understand the differences?

Dave Patterson
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to design a utility class to get values of elements and attributes of xml document which parser can i choose???
thanks in advance
 
Greenhorn
Posts: 3
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Both SAX and DOM are used to parse the XML document. Both has advantages and disadvantages and can be used in our programming depending on the situation.

SAX
• Parses node by node
• Doesn’t store the XML in memory
• We cant insert or delete a node
• SAX is an event based parser
• SAX is a Simple API for XML
• doesn’t preserve comments
• SAX generally runs a little faster than DOM

DOM
• Stores the entire XML document into memory before processing
• Occupies more memory
• We can insert or delete nodes
• Traverse in any direction.
• DOM is a tree model parser
• Document Object Model (DOM) API
• Preserves comments
• SAX generally runs a little faster than DOM

If we need to find a node and doesn’t need to insert or delete we can go with SAX itself otherwise DOM provided we have more memory.
 
Do you pee on your compost? Does this tiny ad?
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic