Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DOM vs XPath - Difference? [closed]

I'm curious of what the pros and cons are of these two, DOM and XPath. What does XPath provide that DOM does not, and the other way around? I'm not looking for performance or anything like that, just what is the main difference between the two when it comes to navigation? When is DOM better to use than XPath for example?

Thanks.

like image 981
Erik Åstrand Avatar asked May 21 '13 13:05

Erik Åstrand


People also ask

Which is fast between DOM and XPath?

My personal experience is, DOM are usually more than 10 times faster than XPath or selector API implementation (e.g. Firefox). However, since XPath accept context node, maybe it is best to select a "stable" parent node with DOM and use XPath for the rest job. This can be both high performance and robust.

What is DOM and XPath?

DOM (Document Object Model) is a representation of a document or document fragment consisting of XML nodes arranged as a tree. XPath is a syntax for expressing a navigation through a DOM to locate one or more nodes.

Is XPath fast?

XPath Locator It is slowest among all locators. But it provides you reliable ways to locate web elements. XPath engines are different in each browser, hence make them inconsistent across browsers. That means if you write XPath for your application in Chrome browser, it may not work on IE.


1 Answers

They are complementary rather than competing. DOM provides a tree model of XML with low-level navigation capability (get first child, get next sibling, etc); XPath adds a much higher-level search and navigation capability (e.g. get the average price of all books, get the title of the last chapter).

Note also that DOM is just one tree model for XML, and is very far from being the best: it's the first and the worst, and it's a shame that so many people still use it. In the Java world there are much better designs available such as JDOM and XOM.

like image 51
Michael Kay Avatar answered Dec 24 '22 05:12

Michael Kay