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

。_饼干妹妹 提交于 2019-11-28 01:47:59
Björn

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.

gioele

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.

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!