5

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.

Erik Åstrand
  • 369
  • 2
  • 5
  • 14
  • 1
    Did you mean to compare DOM and SAX? See [here](http://stackoverflow.com/questions/6828703/difference-about-sax-and-dom) for a comparison. If you can think of DOM as your database, XPath could be the SQL. – Seeta Somagani May 21 '13 at 14:52

1 Answers1

16

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.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164