Java XPath Evaluation Example

Hello Readers,

It’s been a while since my last post. This one will show an example of reading element/attribute content from XML file using XPath utility that comes with Java API.

Below is the XML I used to demo:

<?xml version="1.0" encoding="UTF-8"?>
<note>
 <from>Google</from>
 <to>Apple</to>
 <subject>Friendly Advice</subject>
 <body language="English">Get a life!</body>
</note>

Let’s not focus so much on the content ;).

The XML could have different elements and attributes. So long as you know the hierarchy of the element, you can read the content within. We use XPath expressions to read the content. It’s pretty powerful, you can read more about it here.

Java source does 3 things:

1. Define the XPath expression of elements to be read

2. Create a DOM from XML file

3. Executes evaluate method in XPath class by passing DOM and XPath expression

package com.badalchowdhary.xml;

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

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

public class XPathEval {

 private static final String FROM = "/note/from"; // read element
 private static final String TO = "/note/to";
 private static final String SUBJECT = "/note/subject";
 private static final String BODY = "/note/body";
 private static final String LANGUAGE = "/note/body/@language"; // read an attribute

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

// create a dom from xml file
 DocumentBuilderFactory docFactory = DocumentBuilderFactory
 .newInstance();
 DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
 Document document = docBuilder.parse(new File(
 "./src/com/badalchowdhary/xml/sample.xml"));

// create xpath instance
 XPathFactory xPathFactory = XPathFactory.newInstance();
 XPath xPath = xPathFactory.newXPath();

 String from = xPath.evaluate(FROM, document);
 String to = xPath.evaluate(TO, document);
 String subject = xPath.evaluate(SUBJECT, document);
 String body = xPath.evaluate(BODY, document);
 String language = xPath.evaluate(LANGUAGE, document);

 System.out.println("from: " + from + "\nto: " + to + "\nsubject: "
 + subject + "\nbody: " + body + "\nlanguage: " + language);

}

}

One common use of XPath expressions is when asserting XML data using JUnit.

Looking forward for your feedback.

Your’s Truly

Advertisements

About Badal Chowdhary

I am a Software Engineer by profession. I have done SCJP and SCWCD certifications. Like working on cutting edge technologies and frameworks. Driven by challenges and fascinated by technology. I love playing and watching sports: Cricket, Ping Pong, Tennis, Badminton, Racket Ball and Gym.
This entry was posted in Core Java, XML and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s