Home Tutorials XPath Java - XPath Tutorial
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Java - XPath Tutorial

Advertisement
In this example we have created an XML file "person.xml" first, which is necessary to execute XPath query on to it.

Java - XPath  Tutorial

     

Showing all elements of an XML file using Java xpath

In this example we have created an XML file "person.xml" first, which is necessary to execute XPath query on to it. This "persons.xml" contains information related to name, age, gender of different persons.

Here is the full source code for persons.xml file as follows :

persons.xml

<?xml version="1.0" ?>
<information>
  <person id="1">
  <name>Deep</name>
  <age>34</age>
  <gender>Male</gender>
  </person>
 
 <person id="2">
  <name>Kumar</name>
  <age>24</age>
  <gender>Male</gender>
  </person>
 
  <person id="3">
  <name>Deepali</name>
  <age>19</age>
  <gender>Female</gender>
  </person>

  <!-- more persons... -->
</information>

Now we have declared a class XPathDemo  and in this class we are parsing the XML file with JAXP. First of all we do need to load the document into DOM Document object. We have placed that persons.xml file in that current working directory.

  DocumentBuilderFactory domFactory = 
  DocumentBuilderFactory.newInstance();

  domFactory.setNamespaceAware(true); 
  DocumentBuilder builder = domFactory.newDocumentBuilder();
  Document doc = builder.parse("persons.xml");

Above lines of code parses "persons.xml" file and creates a Document object. Next we have created XPath object with the use of XPathFactory.

XPath xpath = XPathFactory.newInstance().newXPath();

Now we compile the path with the use of compile() method. Finally we will evaluate XPath expression and then we can print all those elements.
Here is the example code for XPathDemo.java as follows:

XPathDemo.java

import org.w3c.dom.*;
import javax.xml.xpath.*;
import javax.xml.parsers.*;
import java.io.IOException;
import org.xml.sax.SAXException;

public class XPathDemo {

  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();
   // XPath Query for showing all nodes value
  XPathExpression expr = xpath.compile("//person/*/text()");

  Object result = expr.evaluate(doc, XPathConstants.NODESET);
  NodeList nodes = (NodeListresult;
  for (int i = 0; i < nodes.getLength(); i++) {
 System.out.println(nodes.item(i).getNodeValue())
  }
  }
}

To run this example follow these steps as mention below :

  1. First create and save an XML file "persons.xml"
  2. Create XPathDemo.java and compile it.
  3. Execute XPathDemo you will get the following output on your console

Output:

Download Source Code

     

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: September 29, 2008

Related Tutorials

Ask Questions?    Discuss: Java - XPath Tutorial   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
vinz
June 26, 2011
great

it works fine... thanks for sharing...
Alfonso
July 25, 2012
Xpath tutorial only one node form select use

Hello, I need only one element to use in select html form (combobox). How I can do?
Joseph
August 3, 2012
Recomendation

Excelent tutorial, it helps me a lot!! Thanks!
DMCA.com