SAX vs. DOM parser


I was studying xmls to create dynamically changing db. I studied to different types of parsers which helped to retrive the information. But which parser to use..? I confused. It depends upon the application we prefere. I copied the difference between them here.

Both SAX and DOM are used to parse the XML document. Both has advantages and disadvantages and can be used in our programming depending on the situation.
Parses node by node Stores the entire XML document into memory before processing
Doesn’t store the XML in memory Occupies more memory
We cant insert or delete a node We can insert or delete nodes
Top to bottom traversing Traverse in any direction.
SAX is an event based parser DOM is a tree model parser
SAX is a Simple API for XML Document Object Model (DOM) API
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
doesn’t preserve comments preserves comments
SAX generally runs a little faster than DOM SAX generally runs a little faster than DOM
If we need to find a node and doesn’t need to insert or delete we can go with SAX itself otherwise DOM provided we have more memory.

12 thoughts on “SAX vs. DOM parser

  1. Pingback: 2010 in review «
  2. It would be cool if you attach an example to this explanation as theoretical is long way different from practical right.

  3. Sweet blog! I found it while searching on Yahoo News.
    Do you have any tips on how to get listed in Yahoo News?
    I’ve been trying for a while but I never seem to get there! Appreciate it

  4. Have you tired Expresso XML Parser? It’s a high performance parser that has a graphical interface. You log onto a website and parse your XML file online so you can create parsing rules without writing code. Then you link up to your existing project using their client code. At the moment they have client code in java and javascript. They have a free developer version at

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s