You tin occupation XPathExpression from javax.xml.xpath bundle to create as well as execute XPATH seem inwards Java. Java API provides javax.xml.xpath package, which contains classes similar Xpath, XpathFactory to piece of work amongst XPATH as well as XML documents. By the means this is 3rd article on Xpath, In final duet of tutorials e.g. XPATH examples to direct values using attributes as well as my XPATH notes for Java programmer, nosotros bring seen basics of Xpath seem from XML as well as Java betoken of view. Anyone who has piece of work amongst XML technologies inwards Java knows importance of XPATH, it is 1 the roughly useful applied scientific discipline to recollect selected information from XML files. XPATH is similar SQL which is used to recollect information from relational database. Many back-office as well as middle-office Java application which transfers information inwards shape of XML documents makes extensive occupation of XPATH seem to recollect value for printing or for determination making. Though at that spot are lot of opened upwardly root XML libraries are available to assist amongst Java as well as XML evolution e.g. XML Beans. I personally prefer to occupation criterion Java API if functionality is available there. In this Java tutorial nosotros volition larn how to create XPath seem inwards Java programme as well as retrieving values from XPATH e.g. text, numbers as well as boolean values, which is critical for programming flow.
Java XPATH seem Example
Creating XPATH seem is simple, only navigate to the node yous wishing to recollect yesteryear putting "/" similar inwards our instance if nosotros wishing to recollect models of each smartphone than nosotros volition write Xpath seem equally /smartphones/smartphone/model. Apart from navigating to desired node inwards Xpath seem nosotros bring equally good used utility method text() to recollect text as well as count() to count lay out of matching results. Java classes from java.xml.xpath bundle e.g. XPath, XPathFactory as well as XPathExpression is used to create as well as evaluate Xpath inwards Java programming language.
/**
* Java programme to demonstrate How to create XPATH expression, How to execute XPATH
* Java programme to demonstrate How to create XPATH expression, How to execute XPATH
* seem to recollect text value, numeric value as well as boolean value.
*
*
*/
package test;
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.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class XPathExample {
public void xPathProcessor() throws SAXException, IOException
package test;
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.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class XPathExample {
public void xPathProcessor() throws SAXException, IOException
, XPathExpressionException, ParserConfigurationException {
//Create DocumentBuilderFactory for reading xml file
DocumentBuilderFactory mill = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();;
Document Dr. = builder.parse("smartphone.xml");
// Create XPathFactory for creating XPath Object
XPathFactory xPathFactory = XPathFactory.newInstance();
// Create XPath object from XPathFactory
XPath xpath = xPathFactory.newXPath();
// Compile the XPath seem for getting all brands
XPathExpression xPathExpr = xpath.compile("/smartphones/smartphone/brand/text()");
// XPath text instance : executing xpath seem inwards java
Object consequence = xPathExpr.evaluate(doc, XPathConstants.NODESET);
System.out.println("Java Xpath text example: All brands of pop smartphones ");
printXpathResult(result);
//get all models yesteryear xpath seem inwards java
xPathExpr = xpath.compile("/smartphones/smartphone/model/text()");
consequence = xPathExpr.evaluate(doc, XPathConstants.NODESET);
System.out.println("Java Xpath text example: All pop smartphone model ");
printXpathResult(result);
// XPath count instance : XPath seem to count elements inwards xml
xPathExpr = xpath.compile("count(/smartphones/smartphone)");
Double count = (Double) xPathExpr.evaluate(doc, XPathConstants.NUMBER);
System.out.println("XPath count example: How many Smartphones nosotros have: ");
System.out.println("Count of elements: " + count);
// XPath conditional exampl e: Do nosotros bring whatever HTC smartphone
xPathExpr = xpath.compile("count(/smartphones/smartphone[brand='HTC']) > 0");
Boolean bear witness = (Boolean) xPathExpr.evaluate(doc, XPathConstants.BOOLEAN);
System.out.println("XPath boolean example: Do nosotros bring whatever HTC smartphone ");
System.out.println(test);
}
public void printXpathResult(Object result){
NodeList nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++) {
System.out.println(nodes.item(i).getNodeValue());
}
}
public static void main(String[] args) throws XpathExpressionException
//Create DocumentBuilderFactory for reading xml file
DocumentBuilderFactory mill = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();;
Document Dr. = builder.parse("smartphone.xml");
// Create XPathFactory for creating XPath Object
XPathFactory xPathFactory = XPathFactory.newInstance();
// Create XPath object from XPathFactory
XPath xpath = xPathFactory.newXPath();
// Compile the XPath seem for getting all brands
XPathExpression xPathExpr = xpath.compile("/smartphones/smartphone/brand/text()");
// XPath text instance : executing xpath seem inwards java
Object consequence = xPathExpr.evaluate(doc, XPathConstants.NODESET);
System.out.println("Java Xpath text example: All brands of pop smartphones ");
printXpathResult(result);
//get all models yesteryear xpath seem inwards java
xPathExpr = xpath.compile("/smartphones/smartphone/model/text()");
consequence = xPathExpr.evaluate(doc, XPathConstants.NODESET);
System.out.println("Java Xpath text example: All pop smartphone model ");
printXpathResult(result);
// XPath count instance : XPath seem to count elements inwards xml
xPathExpr = xpath.compile("count(/smartphones/smartphone)");
Double count = (Double) xPathExpr.evaluate(doc, XPathConstants.NUMBER);
System.out.println("XPath count example: How many Smartphones nosotros have: ");
System.out.println("Count of elements: " + count);
// XPath conditional exampl e: Do nosotros bring whatever HTC smartphone
xPathExpr = xpath.compile("count(/smartphones/smartphone[brand='HTC']) > 0");
Boolean bear witness = (Boolean) xPathExpr.evaluate(doc, XPathConstants.BOOLEAN);
System.out.println("XPath boolean example: Do nosotros bring whatever HTC smartphone ");
System.out.println(test);
}
public void printXpathResult(Object result){
NodeList nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++) {
System.out.println(nodes.item(i).getNodeValue());
}
}
public static void main(String[] args) throws XpathExpressionException
, ParserConfigurationException, SAXException, IOException {
XPathExample xPathExample = new XPathExample();
xPathExample.xPathProcessor();
}
}
Output:
Java Xpath text example: All brands of pop smartphones
XPathExample xPathExample = new XPathExample();
xPathExample.xPathProcessor();
}
}
Output:
Java Xpath text example: All brands of pop smartphones
Apple
Samsung
Nokia
Java Xpath text example: All pop smartphone model
Samsung
Nokia
Java Xpath text example: All pop smartphone model
IPhone4S, IPhone4, IPhone5
Milky Way S2, Milky Way nexus, Milky Way Ace
Lumia 800, Lumia 710, Nokia N9
XPath count example: How many Smartphones nosotros have:
Milky Way S2, Milky Way nexus, Milky Way Ace
Lumia 800, Lumia 710, Nokia N9
XPath count example: How many Smartphones nosotros have:
Count of elements: 3.0
XPath boolean example: Do nosotros bring whatever HTC smartphone
False
XPath boolean example: Do nosotros bring whatever HTC smartphone
False
That’s all on How to create as well as evaluate XPath seem inwards Java . We bring seen How to direct XML elements value equally text using text() business office as well as counted matching elements using count() function. Xpath is existent interesting materials as well as in 1 lawsuit yous acquire the basics, its rattling slow to piece of work amongst Xpath both inwards XML editors as well as Java programming language.
Further Learning
Java In-Depth: Become a Complete Java Engineer!
Master Java Web Services as well as REST API amongst Spring Boot
Difference betwixt SAX as well as DOM parser inwards Java