Byron Kiourtzoglou

About Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.

Find elements by absolute location with XPath

With this example we are going to demonstrate how to find elements by absolute location in XPath. The XPath language provides a simple, concise syntax for selecting nodes from an XML document. In short, to find elements by absolute location in 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, with newXPath() API method of XPathFactory.
  • 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 are searching for the root element. Then we are searching for the root element, using its name. We are also searching for the element under the root element. In these cases the returnType is set to XPathConstants.NODE, and the Node containing the specified element will be returned. Furthermore, we are using expressions to search for all elements under a specified path or search for elements in the document except a specific one. We can also search for elements that have at least one child element or search for elements with a specified level, starting with level one to be the the level of root. In these cases the returnType is set to XPathConstants.NODESET, and a NodeList is returned, that is a collection of the Nodes containing the specified content.

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 FindElementsByAbsoluteLocationWithXPath {

	public static void main(String[] args) throws Exception {
		
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		dbf.setValidating(false);
		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. root element
		expression = "/*";
		node = (Node) xpath.evaluate(expression, doc, XPathConstants.NODE);
		System.out.println("1. " + node.getNodeName());
		
		// 2. root element (by name)
		expression = "/rss";
		node = (Node) xpath.evaluate(expression, doc, XPathConstants.NODE);
		System.out.println("2. " + node.getNodeName());

		// 3. element under rss
		expression = "/rss/channel";
		node = (Node) xpath.evaluate(expression, doc, XPathConstants.NODE);
		System.out.println("3. " + node.getNodeName());
		
		// 4. all elements under rss/channel
		expression = "/rss/channel/*";
		nodeList = (NodeList) xpath.evaluate(expression, doc, XPathConstants.NODESET);
		System.out.print("4. ");
		for (int i = 0; i < nodeList.getLength(); i++) {
			System.out.print(nodeList.item(i).getNodeName() + " ");
		}
		System.out.println();
		
		// 5. all title elements in the document
		expression = "//title";
		nodeList = (NodeList) xpath.evaluate(expression, doc, XPathConstants.NODESET);
		System.out.print("5. ");
		for (int i = 0; i < nodeList.getLength(); i++) {
			System.out.print(nodeList.item(i).getNodeName() + " ");
		}
		System.out.println();
		
		// 6. all elements in the document except title
		expression = "//*[name() != 'title']";
		nodeList = (NodeList) xpath.evaluate(expression, doc, XPathConstants.NODESET);
		System.out.print("6. ");
		for (int i = 0; i < nodeList.getLength(); i++) {
			System.out.print(nodeList.item(i).getNodeName() + " ");
		}
		System.out.println();
		
		// 7. all elements with at least one child element
		expression = "//*[*]";
		nodeList = (NodeList) xpath.evaluate(expression, doc, XPathConstants.NODESET);
		System.out.print("7. ");
		for (int i = 0; i < nodeList.getLength(); i++) {
			System.out.print(nodeList.item(i).getNodeName() + " ");
		}
		System.out.println();
		
		// 8. all level-5 elements (the root being at level 1)
		expression = "/*/*/*/*";
		nodeList = (NodeList) xpath.evaluate(expression, doc, XPathConstants.NODESET);
		System.out.print("8. ");
		for (int i = 0; i < nodeList.getLength(); i++) {
			System.out.print(nodeList.item(i).getNodeName() + " ");
		}
		System.out.println();
		
	}
	
}

Input:

<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
	<channel>
		<title>Java Tutorials and Examples 2</title>
		<language>en-us</language>
		<item>
			<title><![CDATA[Java Tutorials 2]]></title>
			<link>http://www.javacodegeeks.com/</link>
		</item>
		<item>
			<title><![CDATA[Java Examples 2]]></title>
			<link>http://examples.javacodegeeks.com/</link>
		</item>
	</channel>
</rss>

Output:

1. rss
2. rss
3. channel
4. title language item item 
5. title title title 
6. rss channel language item link item link 
7. rss channel item item 
8. title link title link 

  
This was an example of how to find elements by absolute location in XPath in Java.

Related Whitepaper:

Java Essential Training

Author David Gassner explores Java SE (Standard Edition), the language used to build mobile apps for Android devices, enterprise server applications, and more!

The course demonstrates how to install both Java and the Eclipse IDE and dives into the particulars of programming. The course also explains the fundamentals of Java, from creating simple variables, assigning values, and declaring methods to working with strings, arrays, and subclasses; reading and writing to text files; and implementing object oriented programming concepts. Exercise files are included with the course.

Get it Now!  

Examples Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use
All trademarks and registered trademarks appearing on Examples Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

15,153 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books