Tuesday, October 9, 2012

XPATH Java simple example

XPath is used to search any node any element in XML file. ---- very basic defination.

XPath is a fourth generation declarative language for locating nodes in XML documents. This is much more robust than writing the detailed search and navigation code yourself using DOM, SAX, or JDOM. using XPath in a Java program is like using SQL in a Java program. To extract information from a database, you write a SQL statement indicating what information you want and you ask JDBC to fetch it for you. You neither know nor care how JDBC communicates with the database. Similarly with XML, you write an XPath expression indicating what information you want from an XML document and ask the XPath engine to fetch it, without concerning yourself with the exact algorithms used to search the XML document.

1. Suppose I have persons.xml
<?xml version="1.0" ?>
<information>
<person id="1">
<name>Binod</name>
<age>24</age>
<gender>Male</gender>
</person>

<person id="2">
<name>Pramod</name>
<age>22</age>
<gender>Male</gender>
</person>

<person id="3">
<name>Swetha</name>
<age>19</age>
<gender>Female</gender>
</person>
</information>

2. XPathJavaExample.java

import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class XPathJavaExample {
public static void main(String[] args)
throws ParserConfigurationException, SAXException,
IOException, XPathExpressionException {
DocumentBuilderFactory domFactory =
DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true);
DocumentBuilder builder = domFactory.newDocumentBuilder();
Document doc = builder.parse("persons.xml");
XPath xpath = XPathFactory.newInstance().newXPath();
XPathExpression expr = xpath.compile("//person/*/text()");Object result = expr.evaluate(doc, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++) {
System.out.println(nodes.item(i).getNodeValue());
}
}
}

The output of this xpath query ("//person/*/text()");
Binod
24
Male
Pramod
22
Male
Swetha
19
Female

XPathExpression expr = xpath.compile("//person[last()]/*/text()"); This will be give the last record
Swetha
19
Female

XPathExpression expr = xpath.compile("//person/name/text()"); Here name is mentioned instaed of *. * means all the attribute. The output
Binod
Pramod
Swetha
Only name shows here.

XPathExpression expr = xpath.compile("//person[1]/*/text()"); Retrive first record
Binod
24
Male

XPathExpression expr = xpath.compile("//name/text()"); It will show all the name tag, that would be anywhere in xml. The output
Binod
Pramod
Swetha

XPathExpression expr = xpath.compile("//person[age>20]/*/text()"); Output would be
all age that is greater than 20
Binod
24
Male
Pramod
22
Male


There are numbers of xpath expression. You can get from google. Here I showed only few of them. But it is easy to understand .......... :)

0 comments:

Post a Comment