What are the differences between SAX and DOM parsers?

Editorial / Best Answer

prasanna_b2001@yahoo.com  

  • Member Since Dec-2005 | Dec 19th, 2005


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

SAX:

1. Parses node by node

2. Doesnt store the XML in memory

3. We cant insert or delete a node

4. Top to bottom traversing

DOM

1. Stores the entire XML document into memory before processing

2. Occupies more memory

3. We can insert or delete nodes

4. Traverse in any direction.

If we need to find a node and doesnt need to insert or delete we can go with SAX itself otherwise DOM provided we have more memory.

Prasanna Balaraman.

Showing Answers 1 - 43 of 43 Answers

K.Hari Kishore

  • Jun 16th, 2005
 

Hi friends this is hari. SAX Parser is the one which is developed to run the java programs especially. It uses a memory resident model. DOM is developed by w3c consortium so that it is an open standard. In DOM the xml file is arranged as a tree and backward and forward search is possible for searching the tags and evaluating the data which it holds. It depends upon the application and ease of data accessiblity. If we want to extract the data from a xml file once we have to move to SAX, which is one time top to bottom read approach and if we want ot randomly pick the data in an xml file then the tree reperesentation of DOM model is to be put into use. Thats all !

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

SAX:

1. Parses node by node

2. Doesnt store the XML in memory

3. We cant insert or delete a node

4. Top to bottom traversing

DOM

1. Stores the entire XML document into memory before processing

2. Occupies more memory

3. We can insert or delete nodes

4. Traverse in any direction.

If we need to find a node and doesnt need to insert or delete we can go with SAX itself otherwise DOM provided we have more memory.

Prasanna Balaraman.

Sathish Kola

  • May 10th, 2006
 

Hi,

I felt very happy by seaing this comparsions and differences od SAX and DOM

Thanks

Sathish Kola

  Was this answer useful?  Yes

Srinivas A

  • Aug 6th, 2006
 

Thanks All,

Differences are prettey clear.

--Srinivas A

  Was this answer useful?  Yes

Subhash Thakur

  • Aug 10th, 2006
 

Hi Prassanna,Thanks for simple and clear differentiations.regards,Subhash Thakur

  Was this answer useful?  Yes

Kalyan

  • Aug 15th, 2006
 

Thanks Prasanna !! Your explanation was so clear.

  Was this answer useful?  Yes

avinash verma

  • Sep 1st, 2006
 

Hi ,thanks for share your knowledge on SAX and Dom parse . Please send me more article relataed to these parser. I am very thankful to you.

thanks and regards

AvinashV.

  Was this answer useful?  Yes

Guru Brahmam

  • Sep 15th, 2006
 

Hi,

Thanks Prasanna, those are simple and precise differences.

Guru Brahmam

  Was this answer useful?  Yes

sridhar

  • Oct 12th, 2006
 

hai prasanna i am fully satisfied with u r clear differences.

very very very...... thanks to u.

  Was this answer useful?  Yes

Mathan

  • Oct 18th, 2006
 

Hi Prasana..

The differences ae so clear....One small amendment,Like DOM, SAX parser will also allow us to add/update new XML elements.

3. We cant insert or delete a node

  Was this answer useful?  Yes

Prem Kumar

  • Nov 8th, 2006
 

Thanks. Points are very clear. But kindly clarrify whether SAX allows to delete elements?

  Was this answer useful?  Yes

venu

  • Dec 8th, 2006
 

No SAX does not allow to delete nodes/

  Was this answer useful?  Yes

Karthikha Subramanian

  • Dec 13th, 2006
 

Hi ,

    This is Karthikha...

     Hari and Prasanna , ThankYou to your explanations...Thanks Hari for your initiative and Thanks Prasanna for your clear view.

  Was this answer useful?  Yes

L Bhargav Prasad

  • Jan 3rd, 2007
 

So nice of you that you shared this info. Keep it up & Thanks

Bhargav./

  Was this answer useful?  Yes

Kalyani

  • Jan 15th, 2007
 

Hi this is Kalyani..

Thank you very much both of you for sharing it with us...........

  Was this answer useful?  Yes

vivek

  • Feb 6th, 2007
 

thanks a lot for givin a better idea on parsing

  Was this answer useful?  Yes

Ramesh Vasudev

  • May 22nd, 2007
 

Hari you have given SAX is resident model. and Prasanna has given as SAX wont store the XML in its memory but DOM would store the XML in Momory.
So which one is correct, both are contradictory statements. Isn't it?

Regards,
Ramesh Vasudev

  Was this answer useful?  Yes

Kannan

  • Jun 22nd, 2007
 

Hi,

Can anybody give examples for these two parser.
In which situtation we can use this.

  Was this answer useful?  Yes

deepa

  • Aug 7th, 2007
 

DOM parser

1.In DOM parser the representation is in the memory as it is used for the internal representation of an
XML document.
2.It reads the whole document and returns a DOM tree representation of the
XML document.
3.So for smaller xml we can use DOM as memory is the constraint.

SAX parser

1.It is event driven
2.It works incrementally and generates events that are passed to the application.
3.For huge
XML we can go for SAX rather than DOM
4.It does not give any structural representation for the
XML document.

So depending on the application you develop you can choose any of the parser

  Was this answer useful?  Yes

The main difference between DOM and SAX is in the representation of the XML document they use. DOM uses an in-memory tree-like structure, whereas, SAX uses linear event driven approach.

  Was this answer useful?  Yes

The SAX parser we can parse it by tag by tag, SAX Parser will not store in Memory
Where as in DOM parser the Document will be read fully and store it in Memory and Start Parsing.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions