Sunday, November 20, 2011

XPath Tutorials Examples for Beginners and Java Developers

XPath Tutorials for Beginners and Java Developers

XPath Tutorials for Beginners

xpath tutorials for beginners example
1. When you want to extract any information from XML documents, simple way is to use XPath expression

2. XPath is used to query XML document.

3. XPath is similar to SQL, SQL is used to query relational database while XPath is used to query XML documents

4. DOM can also be used to extract information form XML document but its not as easy to write, maintain and debug as XPath expression for example for getting "all books whose author is JK Rolling" in XPath is "//book[author="JK Rolling"]/title" while if you use DOM you need to traverse the file, iterate through nodes and then find the information good 20 line of code.

5. XPath is not a full fledged language there are certain things we can not do in XPAth like we can not find all the authors for which external account database shows royalty is due.

6. Java + XPath can complement each other.

7. Java 5 has a library called javax.xml.xpath for querying XML document using XPath and this library is independent of XML Object model.

8. Xalan and Saxon are two XPath engine and they provide there own API for XPath processing.

9. Before Java 5 XPath API java API was XPAth engine dependent.

10. Different object model in XML are DOM (Document Object Model), JDOM and XOM and XPathFactory uses abstract factory design pattern to support multiple object model.

11. JAXP is the java API for xml processing which contains xml parsers and DOM is API provided by w3c.

12. For easier understanding we can correlate XPath to SQL and javax.xml.xpath to javax.sql

13. Its easy to write query in declarative language e.g. SQL and XPath then imperative language like C and Java.

14. XPath 1.0 has four basic data types "node-set", "number", "boolean" and "string".

15. Most XPath expression, especially location paths return note set.

16. Count (//book) return number of book a number

17. Count (//book) > 5 returns Boolean

18. Generally speaking, an XPath
Number maps to a java.lang.Double
String maps to a java.lang.String
Boolean maps to a java.lang.Boolean
node-set maps to an org.w3c.dom.NodeList

19. evaluate() method of XPath API has return type Object but actual type depends upon the type of XPath expression.

20. Second argument of evaluate() method take the expected return type. Corresponding java types are defined in XPathConstants class as


21. XPathConstants.NODE is a special type which only return single node, so if result of XPath query return more than one node then if this return type is specified it will return first node and if XPath query doesn't return any element than evaluate() will return null. If the requested conversion between Java and XPath can't be made, then evaluate() throws an XPathException.

23. There are some minus points as well with this API; first current implementation of this API on JDK only supports DOM, though you can get other implementation which support JDOM and XOM e.g Saxon implementation. Another drawback of this API is that it’s mostly based on
XPath 1.0, there is not much support for XPath2.0. One more disadvantage which in turn an advantage is this API is object model independent which mean most of interfaces are represented as Object and lack type-safety.

This is very raw XPath tutorial and as I said I have not edited it just presented as it is to preserve the order. I may adjust or modify this article when I get some more time. For Now that’s all on this Xpath tutorial.

Some other Tutorial you may like

1 comment :

kams said...


thanx for the Awesome doc...
i have a question if u can help me...
if we are having two domains for the same site ie one is test domain and second is production domain, for ex: www.qa.mysite.com and www.mysite.com both are same sites with same contents.
my question is that the Xpath of each element of both sites would be the same?

basically i have to make selenium script for testing domain and have to use it for the production domain too...
Waiting for you reply.......thanks in advace


Post a Comment