GeekInterview.com
Series: Subject: Topic:
Question: 7 of 46

What are the differences between SAX and DOM parsers?

Asked by: Interview Candidate | Asked on: Jun 16th, 2005

Editorial / Best Answer

Answered by: prasanna_b2001@yahoo.com

Member Since Dec-2005 | Answered On : 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 - 21 of 21 Answers
K.Hari Kishore

Answered On : 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 !

Yes  1 User has rated as useful.
  
Login to rate this answer.
prasanna_b2001@yahoo.com

Answered On : 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.

Yes  2 Users have rated as useful.
  
Login to rate this answer.
Sathish Kola

Answered On : May 10th, 2006

Hi,

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

Thanks

Sathish Kola

  
Login to rate this answer.
Srinivas A

Answered On : Aug 6th, 2006

Thanks All,

Differences are prettey clear.

--Srinivas A

  
Login to rate this answer.
Subhash Thakur

Answered On : Aug 10th, 2006

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

  
Login to rate this answer.
Kalyan

Answered On : Aug 15th, 2006

Thanks Prasanna !! Your explanation was so clear.

  
Login to rate this answer.
avinash verma

Answered On : 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.

  
Login to rate this answer.
Guru Brahmam

Answered On : Sep 15th, 2006

Hi,

Thanks Prasanna, those are simple and precise differences.

Guru Brahmam

  
Login to rate this answer.
sridhar

Answered On : Oct 12th, 2006

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

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

  
Login to rate this answer.
Mathan

Answered On : 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

  
Login to rate this answer.
Prem Kumar

Answered On : Nov 8th, 2006

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

  
Login to rate this answer.
venu

Answered On : Dec 8th, 2006

No SAX does not allow to delete nodes/

  
Login to rate this answer.
Karthikha Subramanian

Answered On : 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.

  
Login to rate this answer.
L Bhargav Prasad

Answered On : Jan 3rd, 2007

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

Bhargav./

  
Login to rate this answer.
Kalyani

Answered On : Jan 15th, 2007

Hi this is Kalyani..

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

  
Login to rate this answer.
vivek

Answered On : Feb 6th, 2007

thanks a lot for givin a better idea on parsing

  
Login to rate this answer.
Ramesh Vasudev

Answered On : May 22nd, 2007

Hari you have given SAX is resident model. and Prasanna has given as SAX wontstore 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

  
Login to rate this answer.
Kannan

Answered On : Jun 22nd, 2007

Hi,

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

  
Login to rate this answer.
deepa

Answered On : 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

  
Login to rate this answer.
samirshaikh

Answered On : May 6th, 2008

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.

  
Login to rate this answer.
venkateshadhi

Answered On : Oct 30th, 2010

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.

  
Login to rate this answer.

Give your answer:

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

Related Open Questions

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Career Counselling

 Have Career Question?

 Ask Chandra

 Ask Only Career questions.

Follow us:
 

Latest Questions

Ads

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, click "Subscribe".