Saturday 4 May 2013

XPath Tutorials Examples for Beginners and Java Developers

XPath Tutorials Examples for Beginners and Java Developers

XPath Tutorials for Beginners and Java Developers
This XPath Tutorial is collection of my Xpath Notes which I prepared recently when I was working on XML and XPATH. Though I was familiar with XML but not with Xpath and thought to note down bullet points about Xpath to get myself up and running. It’s not quite detailed but gives a nice overview on what is Xpath and how can you use Xpath in Java. I have not edited order of notes and presented it as it is in this XPath Tutorial. It also contains some example of xpath expression to give you an idea of how they look like but its analogous to SQL which is used to retrieve data from tables.

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

How to solve OutOfMemoryError in ANT 

How to Split String in Java Example

10 Example of Enum in Java

What are inbuilt Properties in Apache ANT

Difference between String and StringBuffer in Java

How to convert String to Integer in Java

How to increase Heap Size in Maven and ANT

Please share with your friends if like this article

No comments:

Post a Comment