What Are The Differences Between SAX and DOM Parsers?

For the newbies, SAX stands for Simple API for XML and DOM stands for Document Object Model.
Both of these are APIs to parse XML documents. There is no direct competition between DOM and SAX
as both of them have there own strengths and weaknesses.

The following are some fundamental differnces between a SAX and DOM model of parsing.

SAX:

1. Parses the document on node by node basis.
2. Does not store the XML in memory.
3. We can not insert or delete a node.
4. This model uses top to bottom traversing.
5. This model does not preserve comments.
6. It runs little faster than DOM

DOM

1. Stores the entire XML document into memory before processing
2. Occupies more memory
3. We can insert or delete nodes
4. This model can traverse in any direction.
5. This model preserves comments.
6. It runs slower than SAX model

So, when to choose what model to use?

Here is what you can do.

If you just need to read a node, but do not require to insert/delte node, then use SAX.
If you require node manipulation(insert/delete) nodes, use DOM.

There are various parsers in Java that can be used. The following is a list of few java parsers.

  • Apache Parser [Xerces]
  • Oracle XML Parser
  • Sun XML Parser

All of these parsers provide SAX and DOM parsing mechanisms.