Header Ads

Which is better Xpath or DOM?

The Document Object Model (DOM) is a cross-
platform and language-independent convention for
representing and interacting with objects in HTML,
XHTML and XML documents. Objects in the DOM
tree may be addressed and manipulated by using
methods on the objects.
Ex: var element=document.getElementById("intro");
XPath, the XML Path Language, is a query language
for selecting nodes from an XML document. In
addition, XPath may be used to compute values
 (e.g., strings, numbers, or Boolean values) from the
content of an XML document.
Ex: //input[@id=”xxx”]
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

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.
But as starter I will remember DOM as the database and xpath is the SQL query.
Absolute Xpath :
1) start selection from the document node
2) Starts with //
3) e.g. “/html/body/p” matches all the paragraph elements
Google Search Box : /html/body/div[1]/div[2]/div[1]/div[1]/div[3]/div/div/div/form/fieldset[2]/div/div/div
Relative Xpath  :
1) start selection matching anywhere in the document
2) Starts with /
3) e.g. “//p” matches all the paragraph elements starts with p
Google Search Box : //*[@id='gbqfqwb']

image credit:developer.chrome.com
Powered by Blogger.