sundeep
sundeep

Reputation: 1822

Is XPath much more efficient as compared to DOM and SAX?

I need to parse an xml string and find values of specific text nodes, attribute values etc. I'm doing this in javascript and was using the DOMParser class for the same. Later I was informed that DOM is takes up a lot of memory and SAX is a better option.

Recently I found that XPath too provides a simple way to find nodes.

But I'm not sure which amongst these 3 would be the most efficient way to parse XML. Kindly help....

Upvotes: 24

Views: 15755

Answers (5)

Santhosh Kumar Tekuri
Santhosh Kumar Tekuri

Reputation: 3020

See http://code.google.com/p/jlibs/wiki/XMLDog

We give set of xpaths to XMLDog and ask to sniff some XML document. It uses SAX and with one pass over the document it evaluates all the given XPaths.

Upvotes: 1

Björn
Björn

Reputation: 29381

SAX is a top-down parser and allows serial access to a XML document, and works well for read only access. DOM on the other hand is more robust - it reads the entire XML document into a tree, and is very efficient when you want to alter, add, remove data in that XML tree. XPath is useful when you only need a couple of values from the XML document, and you know where to find them (you know the path of the data, /root/item/challange/text).

SAX: Time efficient when iterating through the document, gives a single pass for every iteration

DOM: Flexible/performance, gives you more ways to work your data

XPath: Time efficient when you only need to read a couple of values

Upvotes: 33

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243459

This document from MSDN provides a wealth of information about optimizing XML processing.

In particular, the XPathDocument class is designed to be more efficient for evaluating XPath expressions than using (the DOM-based) XmlDocument class. The reason is that XPathDocument is a read-only representation of an XML document, while a DOM implementation also covers changing the document.

Using DOM has a not less-important downside that it typically results in complicated, spaghetti-like code that is difficult to understand and maintain.

Upvotes: 2

Pete Kirkham
Pete Kirkham

Reputation: 49311

Unless you're using the research prototype of streaming XPath, it is very likely that your XPath engine is loading everything into memory, so it will have similar characteristics to DOM. So it rather depends on your definition of 'efficiency'. It's certainly easier to use, and the XPath implementations could change to be more efficient, whereas DOM will always have some representation of the whole document on the client machine, and SAX will always be a lot more awkward to program than XPath.

Upvotes: 11

TechTravelThink
TechTravelThink

Reputation: 3094

If you only need to find values of specific text nodes, then XPath. The reason DOM takes up a lot of memory is because it reads in the whole XML and form the tree for the document. SAX is event-based. Hence, based on what you have described, XPath best suits your scenario.

Upvotes: 0

Related Questions