Home » Core Java » xml » XPath » Find elements by id with XPath

About Ilias Tsagklis

Avatar photo
Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.

Find elements by id with XPath

With this example we are going to demonstrate how to find elements by Id using XPath. The XPath language provides a simple, concise syntax for selecting nodes from an XML document. XPath also provides rules for converting a node in an XML document object model (DOM) tree to a boolean, double, or string value. In short, to find elements by Id using XPath you should:

  • Obtain a new instance of a DocumentBuilderFactory, that is a factory API that enables applications to obtain a parser that produces DOM object trees from XML documents.
  • Set the parser produced so as not to validate documents as they are parsed, using setValidating(boolean validating) API method of DocumentBuilderFactory, with validating set to false.
  • Create a new instance of a DocumentBuilder, using newDocumentBuilder() API method of DocumentBuilderFactory.
  • Parse the FileInputStream with the content to be parsed, using parse(InputStream is) API method of DocumentBuilder. This method parses the content of the given InputStream as an XML document and returns a new DOM Document object. 
  • Create an XPathFactory instance to be used to create XPath objects, with newInstance() API method of XPathFactory. 
  • Create a new XPath object, using the underlying object model determined when the XPathFactory was instantiated.
  • Create a String expression and use evaluate(String expression, Object item, QName returnType) API method of XPath in order to evaluate it in the specified Document object. The method returns a result as the specified type.
  • In the example, first we evaluate the expression returning the node containing the id, so XPathConstants.NODE is returned. It returns the Node containing the id. We also evaluate the expression returning the set of nodes containing the id, so the returnType is set to XPathConstants.NODESET. It returns a NodeList containing the collection of Nodes that contain the id. We can also evaluate an expression checking if more than one ids exist in the Document. The returnType is set to XPathConstants.NODESET again, and a NodeList is also returned, containing the collection of Nodes that contain the ids.

Let’s take a look at the code snippet that follows:

package com.javacodegeeks.snippets.core;

import java.io.File;
import java.io.FileInputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class FindElementsByIdWithXPath {
public static void main(String[] args) throws Exception {
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db = dbf.newDocumentBuilder();
		Document doc = db.parse(new FileInputStream(new File("in.xml")));
		XPathFactory factory = XPathFactory.newInstance();
		XPath xpath = factory.newXPath();
		String expression;
		Node node;
		NodeList nodeList;
		// 1. elements with id '1'
		expression = "id('1')";
		node = (Node) xpath.evaluate(expression, doc, XPathConstants.NODE);
		System.out.println("1. " + node.getNodeName());
		// 2. all elements under element with id '1'
		expression = "id('1')/entry";
		nodeList = (NodeList) xpath.evaluate(expression, doc, XPathConstants.NODESET);
		System.out.print("2. ");
		for (int i = 0; i < nodeList.getLength(); i++) {
			System.out.print(nodeList.item(i).getNodeName() + " ");
		// 3. elements with id 1, 2 or 4
		expression = "id('1 2 4')";
		nodeList = (NodeList) xpath.evaluate(expression, doc, XPathConstants.NODESET);
		System.out.print("3. ");
		for (int i = 0; i < nodeList.getLength(); i++) {
			System.out.print(nodeList.item(i).getNodeName() + " ");
		// 4. element that does not exist
		expression = "id('UNKNOWN')";
		node = (Node) xpath.evaluate(expression, doc, XPathConstants.NODE);
		System.out.println("4. " + node);



<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE entries [ <!ELEMENT entry (entry*) >

     <!ATTLIST entry  id    ID    #REQUIRED>
	<entry id="1">
		<entry id="2">
			<entry id="3"/>
	<entry id="4"/>


1. entry
2. entry 
3. entry entry entry 
4. null

This was an example of how to find elements by Id using XPath in Java.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions


Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments