• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

DOM and SAX

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How DOM is different from SAX? Detailed explanation is always welcome
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"chandra",
The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at http://www.javaranch.com/name.jsp . We require names to have at least two words, separated by a space, and strongly recommend that you use your full real name. Please choose a new name which meets the requirements.
Thanks.
 
Frank Carver
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
SAX is a stream-based parsing process. You can easily configure a SAX parse to grab certain tags and/or their contents, and ignore others. This makes it quick and effective for extracting specific information from potentially large XML sources.
DOM is a document-based parsing process. A DOM parser reads a whole XML source into a large, complex internal structure and provides lots of operations to extract, insert and modify the loaded data. DOM is good for when you need to load and transform whole documents, or create new XML documents in memory ready for such a peocess.
 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would like to add that because, as Frank already explained, DOM parses the complete XML document into a large data structure in memory, there are some pretty bad performance issues, especially when you are dealing with larger documents. I have to admit that I have not used SAX myself, but all my co-workers swear by it and I am going to use it in my future projects (if at all possible). Of course, there might be cases where the event-based SAX parser cannot be applied, but I suspect that it will work more often than not, especially if you have some control over the XML that you are processing.
-Mirko

Originally posted by Frank Carver:
SAX is a stream-based parsing process. You can easily configure a SAX parse to grab certain tags and/or their contents, and ignore others. This makes it quick and effective for extracting specific information from potentially large XML sources.
DOM is a document-based parsing process. A DOM parser reads a whole XML source into a large, complex internal structure and provides lots of operations to extract, insert and modify the loaded data. DOM is good for when you need to load and transform whole documents, or create new XML documents in memory ready for such a peocess.


 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another important different is that SAX is read-only while DOM is read-write. This means SAX doesnot have any facility using which one can change the content of the XML document being parsed. On the otherhand, DOM provides mutator methods to get/set XML data and the changes done during parsing are durable.
Perhaps this is the one of the most compelling reason to consider DOM-parsing despite of its performance issues. Ofcourse one can always implement a hybrid-SAX that allows manipulating the XML content. It is not impossible, but may be redundant since DOM is available out there which can do the exact same thing!
Hope that helps,
------------------
Ajith Kallambella M.
Sun Certified Programmer for the Java2 Platform.
 
Frank Carver
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which approach to use also depends on the ultimate use for the data. If you are parsing the XML in order to build some application-specific data structure from it, then using DOM can result in even larger memory requirements, as you duplicate all the information in the DOM while building it.
Don't forget though, that there is a difference between the DOM and a DOM. You can build your own lighter-weight Document Object Model, using SAX or any other simple parsing system. JDOM is an example of this; it's a full document model which is much easier to traverse in Java than using the official DOM interface.
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1)SAX is faster than DOM.
2)SAX is good for large documents because it takes comparitively less memory than Dom.
3)SAX takes less time to read a document where as Dom takes more time.
4)With SAX we can access data but we can't modify data.
5)We can stop the SAX parsing when ever and where ever you want.
6)SAX is sequential parsing but with DOM we can move to back also.
7)To parse machine generated code SAX is better.To parse human readable documents DOM is useful.

[This message has been edited by Murali Mohan (edited June 15, 2001).]
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have an large XML file (1GB) and need to transform it to an another xml file. DOM based approch takes a lot time that's why I'm searching for a SAX based XSLT processor. Now every XSLT processor use SAX and DOM internally but during transformation most of them use DOM internally. Can any one give a XSLT parser name which can transform a big XML file and can give output a big xml file using SAX.
 
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

you are parsing the XML in order to build some application-specific data structure from it,


JAXB can be used.
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Shabbir Rahman:
Can any one give a XSLT parser name which can transform a big XML file and can give output a big xml file using SAX.

This is just guessing, but I don't think there is one that uses SAX. XSL transformation is a complex task (ever seen the XSL specification?), which requires the engine to move back and forth within the document due to even the simplest XPath expressions. In practice, the only viable way I can think of is to keep the whole document in memory (a la DOM).
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just how complex is the re-arrangement you are attempting? It seems to me that this is the basic question.
If you have to compeletely change the hierarchy, its going to be a BIG memory intensive job. If you are just doing a few renamings, adding/removing attributes or changing limited areas of the hierarchy, then you might be able to write a custom SAX based utility or find a XSLT processor that won't use a lot of memory.
Bill
 
Shabbir Rahman
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Apache Trax may be the solution, I will reply after testing it.
 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone provide me with a tutorial for SAX?
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ketan Chachad:
Can anyone provide me with a tutorial for SAX?


Go to http://www.ibm.com/developerworks and search for "Understanding SAX".
reply
    Bookmark Topic Watch Topic
  • New Topic