Solved

SAX vs. DOM

Posted on 2000-05-02
3
1,414 Views
Last Modified: 2008-02-26
Can anyone discuss, in detail, the advantages or disadvantages of using SAX to parse an XML file rather than DOM?  Are there any particular qualities that a file containing XML should have which would make it more suitable for one or the other???
0
Comment
Question by:callapm
3 Comments
 

Expert Comment

by:deanejoyce
ID: 2770274
Basically sax is a serial API and dom needs the whole document model in order to process stuff.....

if you have a large database or distributed datbases containing lots of info, use sax.....it's way quicker.....

if your documents or database are relatively small use dom....

so in a b2b situation I nearly always go for sax unless you are dealing with a small vendor....

I hear the new apache/perl validating parser is pretty good for DOM, so perhaps you should check it out.....

deane

0
 
LVL 44

Expert Comment

by:bruintje
ID: 2770787
Hi callapm,

That's a long discussion, for more perspective

http://www.xml.com/pub/Guide/SAX_vs._DOM

or for SAX
http://www.xml.com/pub/Guide/SAX
or DOM
http://www.xml.com/pub/Guide/DOM

HTH:O)Bruintje
0
 
LVL 27

Accepted Solution

by:
BigRat earned 50 total points
ID: 2770839
The difference lies in processing.

   If you want to simply extract data from an XML document then using SAX is easiest. The method involves sequentially reading the file and when a certain "token" (such as element start or end (<element> and </element>) occurs a particular call back procedure which you supply is called. To boldly go (I couldn't resist that) through a file looking at all <fred>s is particularly easy. It gets more complex when you want to look at fred included in jim. You yourself must provide programming logic to sieve out the events. For example you must set a flag on jim's start, process a fred only when the flag is set, and reset the flag on jim end.
   Note also with SAX you cannot modify the file, and that is a big weakness.

  As Deane said, the DOM parser reads the entire file into memory and constructs a tree. You can walk this tree as you wish. You can delete and create new nodes in the tree, which can correspond to element data or attributes. And of course you can modify all the elements, their attributes and the order of the elements. The MS XML ActiveX object is a DOM parser.

   You should also remember than an XSL stylesheet transformer is effectively a DOM parser.

   A good explaination of both DOM and SAX can be found in Sams Teach Yourself XML in 21 Days (ISBN 1-57521-396-6) (which I did not write but heartily recommend).
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now