Last active
January 24, 2019 07:37
-
-
Save LiWenGu/8af34346113418055a98053bf33e47db to your computer and use it in GitHub Desktop.
[XPath] XPath parse DOM #java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void testXPath() throws Exception { | |
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); | |
documentBuilderFactory.setValidating(true); | |
documentBuilderFactory.setNamespaceAware(false); | |
documentBuilderFactory.setIgnoringComments(true); | |
documentBuilderFactory.setIgnoringElementContentWhitespace(false); | |
documentBuilderFactory.setCoalescing(false); | |
documentBuilderFactory.setExpandEntityReferences(true); | |
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); | |
documentBuilder.setErrorHandler(new ErrorHandler() { | |
@Override | |
public void warning(SAXParseException exception) throws SAXException { | |
System.out.println("WARN:" + exception.getMessage()); | |
} | |
@Override | |
public void error(SAXParseException exception) throws SAXException { | |
System.out.println("error:" + exception.getMessage()); | |
} | |
@Override | |
public void fatalError(SAXParseException exception) throws SAXException { | |
System.out.println("fatalError:" + exception.getMessage()); | |
} | |
}); | |
Document doc = documentBuilder.parse(XPathTest.class.getClass().getResourceAsStream("/inventory.xml")); | |
XPathFactory factory = XPathFactory.newInstance(); | |
XPath xPath = factory.newXPath(); | |
XPathExpression expr = xPath.compile("//book[author='Neal Stephenson']/title/text()"); | |
Object result = expr.evaluate(doc, XPathConstants.NODESET); | |
System.out.println("查询作者为 Neal Stephenson 的图书标题是:"); | |
NodeList nodeList = (NodeList) result; | |
for (int i = 0; i < nodeList.getLength(); i++) { | |
System.out.println(nodeList.item(i).getNodeValue()); | |
} | |
System.out.println("查询 1997 年之后的图书的标题"); | |
nodeList = (NodeList) xPath.evaluate("//book[@year>1997]/title/text()", doc, XPathConstants.NODESET); | |
for (int i = 0; i < nodeList.getLength(); i++) { | |
System.out.println(nodeList.item(i).getNodeValue()); | |
} | |
System.out.println("查询 1997 年之后的图书的属性和标题:"); | |
nodeList = (NodeList) xPath.evaluate("//book[@year>1997]/@*|//book[@year>1997]/title/text()", doc, XPathConstants.NODESET); | |
for (int i = 0; i < nodeList.getLength(); i++) { | |
System.out.println(nodeList.item(i).getNodeValue()); | |
} | |
} | |
<!-- book.xml --> | |
<inventory> | |
<book year="2000"> | |
<title>Snow Crash</title> | |
<author>Neal Stephenson</author> | |
<publisher>Spectra</publisher> | |
<isbn>0553380958</isbn> | |
<price>14.95</price> | |
</book> | |
<book year="2005"> | |
<title>Burning Tower</title> | |
<author>Larry Niven</author> | |
<author>Jerry Pournelle</author> | |
<publisher>Pocket</publisher> | |
<isbn>0743416910</isbn> | |
<price>5.99</price> | |
</book> | |
<book year="1995"> | |
<title>Zodiac</title> | |
<author>Neal Stephenson</author> | |
<publisher>Spectra</publisher> | |
<isbn>0553573862</isbn> | |
<price>7.50</price> | |
</book> | |
<!--more books ... --> | |
</inventory> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment