• 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

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anybody tell when to go for SAX and DOM?

I know that even SAX is preffered to when reading Long XML docs.
Others that SAX is also prefered when data is not in XML format
Can anybody tell me abt no-XML format.


Niranjan
 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use SAX parser when
- Input document is too big for available memory.
- When only a part of the document is to be read and we create the data structures of our own.
- If you use SAX, you are using much less memory and performing much less dynamic memory allocation.

Use DOM when
- Your application has to access various parts of the document and using your own structure is just as complicated as the DOM tree.
- Your application has to change the tree very frequently and data has to be stored for a significant amount of time.
 
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
basically SAX is strictly read-only so if you need to modify the XML document in any form (or create one from scratch) you MUST use DOM.

Then there's the potential problem of repeated access to elements which is impossible in SAX, so if you need to do things which require you to process something more than once you will again need DOM (or use your own caching mechanism on top of SAX).

Then there's personal preference. I just like DOM better, to me it's more intuitive and easier to use than is SAX.
 
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

basically SAX is strictly read-only so if you need to modify the XML document in any form (or create one from scratch) you MUST use DOM.


I can't agree with that. You can read using SAX, perform logical operations and write a new document using the data in SAX events. Naturally you are limited in the kinds of logic you can perform due to the streaming order of the SAX events.
For example, modifying the attributes in selected XML elements is easily done in a startElement method.
There are many other ways to create an XML document besides building a DOM in memory.
Bill
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic