Introduction
XPath is a query language for traversinig through XML files. It is a syntax for defining parts of XML documents. It uses a path expression to navigate through XML documents.
Xpath expressions are of great help when we want to operate on a specific node in an XML document. I will be creating a XML file and then, I will create a Java class to read that file and use XPath expressions, to navigate through elements in XML. The following procedure will help you in performing some operations.
1. I have created a XML file named
file.xml and placed it in my E drive. The File.xml content is:
- < Technologies > < Node0 > .Net < Node1 > Abstract < Node2 > Class < Node3 > Oops < /Node3></Node2 > < /Node1><Node1>CLR</Node1 > < /Node0><Node0>Java</Node0 > < Node0 > PHP < /Node0><Node0>C#</Node0 > < /Technologies>
2. Now, we will create a Java class file to read this XML file and then, do various XPath operations on this file. The code to read the XML file is:
- File f = new File("E:\\file.xml");
- Document doc = null;
- DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
- DocumentBuilder docBuilder;
- docBuilder = docFactory.newDocumentBuilder();
- doc = docBuilder.parse(f);
- doc.getDocumentElement().normalize();
3. Now, we will create an instance of XPath and then, will use expressions to navigate through the XML
- XPath xpath = XPathFactory.newInstance().newXPath();
- The following is an Expression to get the root element of the XML file.
- NodeList name = (NodeList) xpath.compile("/*").evaluate(doc, XPathConstants.NODESET);
- This expression will return a NodeList and we will get the element name of the root node, used for a loop. The code is as follows:
- for (int i = 0; i < name.getLength(); i++) {
- System.out.println(name.item(i).getNodeName());
- }
The output after the running this code is:
Technologies
- The following is an Expression to get the text of all Node0 elements.
- NodeList name = (NodeList) xpath.compile("//Node0/text()").evaluate(doc, XPathConstants.NODESET);
This expression will return a NodeList and we can get the text content of all the nodes used for a loop. The code is as follows.
- for (int i = 0; i < name.getLength(); i++) {
- System.out.println(name.item(i).getTextContent());
- }
The output after running this code is:
.Net
Java
PHP
C#
- The following, is an Expression to get all the child nodes inside of a specific node (Node0).
- NodeList name = (NodeList) xpath.compile("//Node0/*").evaluate(doc, XPathConstants.NODESET);
This expression will return a NodeList and we can get the text content, of all the nodes, using a for loop. The code is as follows:
- for (int i = 0; i < name.getLength(); i++) {
- System.out.println(name.item(i).getTextContent());
- }
The output after running this code is:
Abstract
Class
Oops
CLR
- The following is an Expression, to get the element names of all the children, of a specific node (Node0).
- NodeList name = (NodeList) xpath.compile("//Node0/*").evaluate(doc, XPathConstants.NODESET);
This expression will return a NodeList and we get the element name of the root node, used for a loop. The code is as follows:
- for (int i = 0; i < name.getLength(); i++) {
- System.out.println(name.item(i).getNodeName());
- }
The output after the running this code is:
Node1
Node1
I will be coming up with XPath operators and more expressions in future articles. I hope this article was useful to you.