XPath - Java working with XML overview (tutorial)

Complete Source Code for all examples below

XPath is quite a powerfull tool when you use it correctly. In a nutshell it provides means of selecting nodes or node values from Document or a Document Node by specifying a path.

XPath can be quite decieving when you use it incorrectly, so I would strongly advise to learn the XPath w3c spec before doing anything major.

Points to consider:

  • Works on Fully loaded DOM Object
  • Allows flexible way of extracting data from DOM
  • Must be handled with care since even a slightest mistake in path can lead to unpredictable results
  • Should be used when the data to be extracted cannot be known in advance.

 

Simple Example:

Extract SYMBOLS data from warehouse XML in order to display list of available SKUs (Stock Keeping Unit).

 

The XML:

 



  
    PIPE
    Smoking pipe
    10.90
    10
  
  
    VIO
    Violin
    99.99
    5
  



 

The Code:

 

package dp.test.xml.xpath;

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.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 * XPath can only be use with fully loaded DOM or at least a Node.
 * 
 * @author DPavlov
 */
public class XPathExample {
	
	@Test
	public void testSimpleXPathExample() throws XPathExpressionException, ParserConfigurationException, SAXException, IOException {
		
		// XPath object is compiled for reuse
		XPathFactory factory = XPathFactory.newInstance();
		XPath xpath = factory.newXPath();
		XPathExpression expression = xpath.compile("/warehouse/stock/symbol/text()");
		
		// Use DOM API to get full XML document
		DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
		Document document = docBuilder.parse(XPathExample.class.getResourceAsStream("stock.xml"));

		final String result = (String) expression.evaluate(document, XPathConstants.STRING);
		
		System.out.println("STRING: " + result);
		
		final NodeList resultNL = (NodeList) expression.evaluate(document, XPathConstants.NODESET);
		
		for (int i = 0; i < resultNL.getLength(); i++) {
			System.out.println("NODE[" + i + "]: " + resultNL.item(i).getTextContent());
		}
		
	}

}

 

The output:

STRING: PIPE
NODE[0]: PIPE
NODE[1]: VIO

Summary:

  • The document can be loaded in the standard fashion through DOM approach.
  • The XPaths objects are created using XPathFactory
  • XPaths need to be compiled before use by using XPath.compile(String) that results in XPathExpression
  • It might be good idea to cache XPathExpression to avoid compilation overhead.
  • XPathExpression can be used on any DOM object
  • Result of evaluation depends on the type of constant supplied to evaluate method of the expression

 

This page was last updated on: 13/04/2012 11:09