mirror of
https://github.com/Estom/notes.git
synced 2026-02-04 11:04:21 +08:00
Java内容重新整理删除过期的东西
This commit is contained in:
33
Java/JavaDemo/codedemo/xml/BookStore.xml
Normal file
33
Java/JavaDemo/codedemo/xml/BookStore.xml
Normal file
@@ -0,0 +1,33 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<bookstore>
|
||||
<!-- 中文书本信息 -->
|
||||
<book>
|
||||
<title lang="cn">Java并发编程实战</title>
|
||||
<author>Brian Goetz / Tim Peierls / Joshua Bloch / Joseph Bowbeer / David Holmes / Doug Lea</author>
|
||||
<year>2012</year>
|
||||
<price>69</price>
|
||||
</book>
|
||||
|
||||
<!-- 英文书本信息 -->
|
||||
<book>
|
||||
<title lang="en">Java Concurrency in Practice</title>
|
||||
<author>Brian Goetz / Tim Peierls / Joshua Bloch / Joseph Bowbeer / David Holmes / Doug Lea</author>
|
||||
<year>2006</year>
|
||||
<price>59.99</price>
|
||||
</book>
|
||||
|
||||
<book>
|
||||
<title lang="de">德文书</title>
|
||||
<author>德国</author>
|
||||
<year>2014</year>
|
||||
<price>50</price>
|
||||
</book>
|
||||
|
||||
<book>
|
||||
<title>没有lang属性</title>
|
||||
<author>试试</author>
|
||||
<year>2016</year>
|
||||
</book>
|
||||
|
||||
</bookstore>
|
||||
34
Java/JavaDemo/codedemo/xml/XPathDemo.java
Normal file
34
Java/JavaDemo/codedemo/xml/XPathDemo.java
Normal file
@@ -0,0 +1,34 @@
|
||||
package cn.aofeng.demo.xml;
|
||||
|
||||
import javax.xml.xpath.XPath;
|
||||
import javax.xml.xpath.XPathConstants;
|
||||
import javax.xml.xpath.XPathExpressionException;
|
||||
import javax.xml.xpath.XPathFactory;
|
||||
|
||||
import org.w3c.dom.Node;
|
||||
import org.w3c.dom.NodeList;
|
||||
import org.xml.sax.InputSource;
|
||||
|
||||
/**
|
||||
* XPath语法实践。
|
||||
*
|
||||
* @author <a href="mailto:aofengblog@163.com">聂勇</a>
|
||||
*/
|
||||
public class XPathDemo {
|
||||
|
||||
public static void main(String[] args) throws XPathExpressionException {
|
||||
InputSource ins = new InputSource(XPathDemo.class.getResourceAsStream("/cn/aofeng/demo/xml/BookStore.xml"));
|
||||
XPathFactory factory = XPathFactory.newInstance();
|
||||
XPath xpath = factory.newXPath();
|
||||
NodeList result = (NodeList) xpath.evaluate("//title[@lang='de']", ins, XPathConstants.NODESET);
|
||||
for (int i = 0; i < result.getLength(); i++) {
|
||||
Node node = result.item(i);
|
||||
StringBuilder buffer = new StringBuilder()
|
||||
.append("NodeName=").append(node.getNodeName()).append(", ")
|
||||
.append("NodeValue=").append(node.getNodeValue()).append(", ")
|
||||
.append("Text=").append(node.getTextContent());
|
||||
System.out.println(buffer.toString());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user