Solved

SAX vs. DOM

Posted on 2000-05-02
3
1,417 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
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 Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

831 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