Share on Google+Share on Google+

Java - XPath Tutorial

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 :


<?xml version="1.0" ?>
  <person id="1">
 <person id="2">
  <person id="3">

  <!-- more persons... -->

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 = 

  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 as follows:

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

public class XPathDemo {

  public static void main(String[] args
 throws ParserConfigurationException, SAXException, 
  IOException, XPathExpressionException {

  DocumentBuilderFactory domFactory = 
  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++) {

To run this example follow these steps as mention below :

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


Download Source Code




Posted on: September 29, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

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.

Discuss: Java - XPath Tutorial   View All Comments

Post your Comment

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

it works fine... thanks for sharing...
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?
August 3, 2012

Excelent tutorial, it helps me a lot!! Thanks!