XML XPath tutorial

XML XPath tutorial

This tutorial will show you how to parse and read XML file using XPath using java.

XPath full description is XML Path Language. It is query language in XML to select nodes from document. XPath can be used to extract all values from XML document.

It is based on tree representation of XML document that provides ability to navigate through the tree and fire XPath various expression to select. This was initially inspired by need to provide common syntax and behavior model between XSLT and XPointer, which is subsets of XPath query language and it is used in W3C specifications example on XForms, XML Schema and Internationalization Tag Set. Now XPath is adopted by a numeral of XML processing libraries and tools many of them also offer W3C standard, CSS Selectors as alternative to XPath.

Below is sample XML file which we will parse and read. This XML file contains information about movie which is available on Redbox. In the end you could also download XML file and java class for your test.

<?xml version="1.0" encoding="UTF-8"?>
<Redbox>

<Movie category="Kids">
  <title lang="en">Froze</title>
  <Director>Chris Buck</Director>
  <year>2013</year>
  <price>24.00</price>
</Movie>

<Movie category="Action">
  <title lang="en">Homefront</title>
  <Director>Gary Fleder</Director>
  <year>2013</year>
  <price>20.45</price>
</Movie>

<Movie category="Drama">
  <title lang="en">August: Osage County</title>
  <Director>John Wells</Director>
  <year>2013</year>
  <price>19.99</price>
  <Star>Meryl Streep, Julia Roberts, Ewan 
  McGregor, Chris Cooper, Abigail Breslin</Star>
</Movie>

</Redbox>

Below are steps to parse and read XML file:

XML XPath tutorial step 1: Load XML file and parse using DOM parser and create document object as below:

File xmlFile = new File("resources/RedboxMovieStore.xml");

DocumentBuilderFactory builderFactory = DocumentBuilderFactory
        .newInstance();

DocumentBuilder builder = builderFactory.newDocumentBuilder();

Document xmlDocument = builder.parse(xmlFile);

XML XPath tutorial step 2: Create XPath object

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

XML XPath tutorial step 3: Finally create expression and read XML file. Below is detail sample code:

package com.javahonk.xpath;

import java.io.File;
import java.io.FileNotFoundException;
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.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class ReadXMLFileUsingXPath {

    public static void main(String[] args) {
        try {
            File xmlFile = new File("resources/RedboxMovieStore.xml");

            DocumentBuilderFactory builderFactory = DocumentBuilderFactory
                    .newInstance();

            DocumentBuilder builder = builderFactory.newDocumentBuilder();

            Document xmlDocument = builder.parse(xmlFile);

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

            System.out.println("Select all movie title");
            String expression = "/Redbox/Movie/title";
            NodeList nodeList = (NodeList) xPath.compile(expression)
                    .evaluate(xmlDocument, XPathConstants.NODESET);
            for (int i = 0; i < nodeList.getLength(); i++) {
                System.out.println(nodeList.item(i)
                        .getFirstChild().getNodeValue()); 
            }

            System.out.println("Select movie price>15");
            expression = "/Redbox/Movie[price>15]/price";
            nodeList = (NodeList) xPath.compile(expression)
                    .evaluate(xmlDocument, XPathConstants.NODESET);
            for (int i = 0; i < nodeList.getLength(); i++) {
                System.out.println(nodeList.item(i)
                        .getFirstChild().getNodeValue()); 
            }

            System.out.println("Select title where price>15");
            expression = "/Redbox/Movie[price>15]/title";
            nodeList = (NodeList) xPath.compile(expression)
                    .evaluate(xmlDocument, XPathConstants.NODESET);
            for (int i = 0; i < nodeList.getLength(); i++) {
                System.out.println(nodeList.item(i)
                        .getFirstChild().getNodeValue()); 
            }

            System.out.println("Select movie name where category is Kids");
            expression = "/Redbox/Movie[@category='Kids']/title";
            nodeList = (NodeList) xPath.compile(expression)
                    .evaluate(xmlDocument, XPathConstants.NODESET);
            for (int i = 0; i < nodeList.getLength(); i++) {
                System.out.println(nodeList.item(i)
                        .getFirstChild().getNodeValue()); 
            } 

            System.out.println("Select movie where category is kids");
            expression = "/Redbox/Movie[@category='Kids']";
            Node node = (Node) xPath.compile(expression)
                    .evaluate(xmlDocument, XPathConstants.NODE);
            if(null != node) {
                nodeList = node.getChildNodes();
                for (int i = 0;null!=nodeList && 
                        i < nodeList.getLength(); i++) {
                    Node nod = nodeList.item(i);
                    if(nod.getNodeType() == Node.ELEMENT_NODE)
                        System.out.println(nodeList.item(i)
                                .getNodeName() + " : " 
                                + nod.getFirstChild().getNodeValue()); 
                }
            }

            System.out.println("Select movie directors");
            expression = "/Redbox/Movie/Director";
            nodeList = (NodeList) xPath.compile(expression)
                    .evaluate(xmlDocument, XPathConstants.NODESET);
            for (int i = 0; i < nodeList.getLength(); i++) {
                System.out.println(nodeList.item(i)
                        .getFirstChild().getNodeValue()); 
            }

            System.out.println("Select director name "
                    + "where category = Action");
            expression = "/Redbox/Movie[@category='Action']/Director";
            nodeList = (NodeList) xPath.compile(expression)
                    .evaluate(xmlDocument, XPathConstants.NODESET);
            for (int i = 0; i < nodeList.getLength(); i++) {
                System.out.println(nodeList.item(i)
                        .getFirstChild().getNodeValue()); 
            }

            System.out.println("Select all category");            
            expression = "/Redbox/Movie/@category";
            nodeList = (NodeList) xPath.compile(expression)
                    .evaluate(xmlDocument, XPathConstants.NODESET);
            for (int i = 0; i < nodeList.getLength(); i++) {
                System.out.println(nodeList.item(i)
                        .getFirstChild().getNodeValue()); 
            }

            System.out.println("Select first category");            
            expression = "/Redbox/Movie[1]/@category";
            nodeList = (NodeList) xPath.compile(expression)
                    .evaluate(xmlDocument, XPathConstants.NODESET);
            for (int i = 0; i < nodeList.getLength(); i++) {
                System.out.println(nodeList.item(i)
                        .getFirstChild().getNodeValue()); 
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XPathExpressionException e) {
            e.printStackTrace();
        }
    }

}

 

XML XPath tutorial Output:

XPath_tutorial

download2 Download source code: XPathTutorial

 

Another example to read XML file:

  • We will read below XML file:
<?xml version="1.0" encoding="UTF-8"?>
<persons>
    <person id="1">
        <firstName>Java</firstName>
        <lastName>Honk</lastName>
        <address>
            <streetName>John st.</streetName>
            <location>NY</location>
            <city>NY</city>
            <zip>12345</zip>
        </address>
    </person>
    
    <person id="2">
        <firstName>Java</firstName>
        <lastName>Honk</lastName>
        <address>
            <streetName>John st.</streetName>
            <location>NY</location>
            <city>NY</city>
            <zip>12345</zip>
        </address>
    </person>
</persons>

  • Java class:
package com.javahonk;

import java.io.File;
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.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class ReadXMLXPath {

    public static void main(String[] args) {

        
        try {
            
            File xmlFile = new File("src/main/resources/Persons.xml");

            DocumentBuilderFactory builderFactory = 
                    DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = builderFactory
                    .newDocumentBuilder();
            Document xmlDocument = builder.parse(xmlFile);

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

            String expression = "/persons/person";
            NodeList nodeList = (NodeList) xPath.compile(expression)
                    .evaluate(xmlDocument, XPathConstants.NODESET);
            
           for(int i=0; i <nodeList.getLength() ; i++) {

                Node node = nodeList.item(i);
                if(node.getNodeType() == Node.ELEMENT_NODE) {

                    Element element = (Element)node;                              
                    System.out.println("Ids :"+element
                            .getAttribute("id"));                    
                    System.out.println("First Name : "+ element
                            .getElementsByTagName("firstName")
                            .item(0).getTextContent());
                    System.out.println("Last Name : "+ element
                            .getElementsByTagName("lastName")
                            .item(0).getTextContent());
                    System.out.println("\nAddress :");
                    System.out.println("Street Name : "+ element
                            .getElementsByTagName("streetName")
                            .item(0).getTextContent());
                    System.out.println("Location : "+ element
                            .getElementsByTagName("location")
                            .item(0).getTextContent());
                    System.out.println("City : "+ element
                            .getElementsByTagName("city")
                            .item(0).getTextContent());
                    System.out.println("Zip : "+ element
                            .getElementsByTagName("zip")
                            .item(0).getTextContent());
                    System.out.println("\n");
                }
            }
            
            
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XPathExpressionException e) {
            e.printStackTrace();
        }

        
    }

}

Output:

XML XPath tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *