• 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

SAX or DOM

 
Ranch Hand
Posts: 625
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm creating a news applet that reads from an xml file. I need to extract only certain elements from the file, and the text that goes with those elements. I've looked at both models, but I wasn't sure which would serve my purpose better. I'd appreciate any suggestions and why one would be easier than the other. Thanks a lot.
 
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For simple extraction SAX would be faster and require less memory. I usually think of DOM as mostly useful for transformation or direct representation of XML.
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use SAX if -
  • You can extract all that you need in one pass.
  • While processing the elements, you dont need to "look back" at the ancestor elements.


  • Use DOM if -
  • You need to have random access to different parts of the document.
  • Your processing logic requires traversing the document more than once, possibly with different starting points.
  • The processing logic is likely to modify the original document by adding/removing/moving elements.
  • The processing involves creating new documents in memory.


  • As you can see, SAX and DOM has their own plus points. Performance is another thing you should consider. Since DOM parsing creates an image of the document in memory, it consumes more resources. SAX on the otherhand is really lightweight and provides very good response time for large documents( > 1MB ), but it is not as flexible as DOM is.
    It is not unusual for applications to use both DOM and SAX parsing depending on what is being done with the XML document.
    Hope that helps.
     
    This cake looks terrible, but it tastes great! Now take a bite out of this tiny ad:
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic