XPath is the XML Path language (XML Path Language), which is a language used to determine the location of a part of an XML document. XPath is based on an XML tree structure that provides the ability to find nodes in a data structure tree. At first the original idea of XPath was to use it as a generic syntax model between XPointer and XSL. But XPath was quickly used by developers as a small query language.
Xpathtest.java
Copy Code code as follows:
Package com.hongyuan.test;
Import Java.io.File;
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.XPathExpressionException;
Import Javax.xml.xpath.XPathFactory;
Import org.w3c.dom.Document;
Import Org.w3c.dom.Node;
Import org.w3c.dom.NodeList;
Import org.xml.sax.SAXException;
public class Xpathtest {
public static void Main (string[] args) throws Parserconfigurationexception,
Saxexception, IOException, xpathexpressionexception {
Parsing a file, generating a Document object
Documentbuilder builder = documentbuilderfactory.newinstance ()
. Newdocumentbuilder ();
Document document = Builder.parse (new File ("Bookstore.xml"));
To generate an XPath object
XPath XPath = xpathfactory.newinstance (). Newxpath ();
Get node value
String webtitle = (string) xpath.evaluate (
"/bookstore/book[@category = ' WEB ']/title/text ()", document,
xpathconstants.string);
System.out.println (Webtitle);
System.out.println ("===========================================================");
Get Node Property value
String Webtitlelang = (string) xpath.evaluate (
"/bookstore/book[@category = ' WEB ']/title/@lang", document,
xpathconstants.string);
System.out.println (Webtitlelang);
System.out.println ("===========================================================");
Get Node Object
Node Bookweb = (node) xpath.evaluate (
"/bookstore/book[@category = ' WEB ']", document,
Xpathconstants.node);
System.out.println (Bookweb.getnodename ());
System.out.println ("===========================================================");
Get node Collection
NodeList books = (nodelist) xpath.evaluate ("/bookstore/book", document,
Xpathconstants.nodeset);
for (int i = 0; i < books.getlength (); i++) {
Node book = Books.item (i);
System.out.println (Xpath.evaluate ("@category", book,
xpathconstants.string));
}
System.out.println ("===========================================================");
}
}
Bookstore.xml
Copy Code code as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<bookstore>
<book category= "Cooking" >
<title lang= "en" >everyday italian</title>
<author>giada De laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category= "CHILDREN" >
<title lang= "en" >harry potter</title>
<author>j K. rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category= "WEB" >
<title lang= "en" >learning xml</title>
<author>erik T. ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
Run effect