Question:
What are the differences between SAX and DOM parsers?
Answer:
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.
Source: CoolInterview.com
DOM Loads whole document into memory.So it requires lot of memory. Not recommended for large XML Document.Using DOM you can traverse thru an XML document. To parse XML in .NET enviornment using Document object model,use XmlDocument and XmlElement. These classes are available in System.Xml namespace
Using this classes,we can traverse an XML document and add,modify its element.
Creating XmlDocument XmlDocument newXmlDoc = new XmlDocument(); Load Xml Document newXmlDoc.Load(XmlFileName); To Create Element XmlElement newXmlElement = newXmlDoc.CreateElement("NEWELEMENT"); XmlElement newXmlMain = newXmlDoc.CreateElement("MAIN"); To Add Child newXmlMain.AppendChild (newXmlElement); Save Document newXmlDoc.Save();
2.Simple API for XML (SAX)
Load/Read portion of XML document as needed.This is the best option when memory is limited.Cannot traverse backward thru an XML file.Speed is very less compare to DOM
Source: CoolInterview.com
Answered by: Ruchi | Date: 2/10/2009
| Contact Ruchi
If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-
- There should not be any Spelling Mistakes.
- There should not be any Gramatical Errors.
- Answers must not contain any bad words.
- Answers should not be the repeat of same answer, already approved.
- Answer should be complete in itself.
|