Java example to evaluate xpath on string and return result XML in string itself.
1. XPath example – Evaluate xpath on xml string
- Create
org.xml.sax.InputSource
containing withStringReader
referencing to XML string. - Create
XPath
fromXPathFactory
. - Use
xpath.evaluate('expression', inputSource)
to get result HTML.
package com.howtodoinjava.demo; import java.io.StringReader; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; 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.w3c.dom.Document; import org.w3c.dom.NodeList; import org.xml.sax.InputSource; public class XPathExample { public static void main(String[] args) throws Exception { String xml = "<employees>" + "<employee id=\"1\">" + "<firstName>Lokesh</firstName>" + "<lastName>Gupta</lastName>" + "<department><id>101</id><name>IT</name></department>" + "</employee>" + "</employees>"; InputSource inputXML = new InputSource( new StringReader( xml ) ); XPath xPath = XPathFactory.newInstance().newXPath(); String result = xPath.evaluate("/employees/employee/firstName", inputXML); System.out.println(result); } }
Program output:
Lokesh
2. XPath example – Evaluate xpath on xml file
package com.howtodoinjava.demo; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; 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.w3c.dom.Document; import org.w3c.dom.NodeList; public class XPathExample { public static void main(String[] args) throws Exception { String xmlFile = "employees.xml"; //Get DOM DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document xml = db.parse(xmlFile); //Get XPath XPathFactory xpf = XPathFactory.newInstance(); XPath xpath = xpf.newXPath(); //Get first match String name = (String) xpath.evaluate("/employees/employee/firstName", xml, XPathConstants.STRING); System.out.println(name); //Lokesh //Get all matches NodeList nodes = (NodeList) xpath.evaluate("/employees/employee/@id", xml, XPathConstants.NODESET); for (int i = 0; i < nodes.getLength(); i++) { System.out.println(nodes.item(i).getNodeValue()); //1 2 } } }
Program output:
Lokesh 1 2
The input xml file is:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <employees> <employee id="1"> <firstName>Lokesh</firstName> <lastName>Gupta</lastName> <department> <id>101</id> <name>IT</name> </department> </employee> <employee id="2"> <firstName>Brian</firstName> <lastName>Schultz</lastName> <department> <id>102</id> <name>HR</name> </department> </employee> </employees>
In above Xpath tutorial, we learned to evaluate xpath on string with example.
Happy Learning !!
Read More:
Java xpath example from file
Java xpath tutorial
How to get attribute value in xml using xpath in java
Was this post helpful?
Let us know if you liked the post. That’s the only way we can improve.