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

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


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


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.


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.