Skip to content

Commit 16ab898

Browse files
committed
使用XPath查找XML中的元素
1 parent 351370f commit 16ab898

3 files changed

Lines changed: 70 additions & 0 deletions

File tree

‎README.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@
115115
* [Spring IBatis Struts2 集成之一:Spring与IBatis集成 | Spring IBatis Struts2 Integration I: Spring Integrates with IBatis](http://aofengblog.blog.163.com/blog/static/6317021201121911105510/)
116116
* [Spring IBatis Struts2 集成之二:Spring与Struts2集成 | Spring IBatis Struts2 Integration II: Spring Integrates with Struts2](http://aofengblog.blog.163.com/blog/static/6317021201122172525296/)
117117

118+
##XML
119+
* [XPath](src/cn/aofeng/demo/xml/XPathDemo.java)
120+
118121
## EJB
119122
* [EJB3 - 使用Eclipse和JBoss开发和部署Session Bean | How to create Session Bean using JBoss and Eclipse](http://aofengblog.blog.163.com/blog/static/6317021201121310318552/)
120123
* [EJB3 - 使用Eclipse和JBoss开发和部署Entity Bean | How to create Entity Bean using JBoss and Eclipse](http://aofengblog.blog.163.com/blog/static/631702120112147279187/)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<bookstore>
4+
<!-- 中文书本信息 -->
5+
<book>
6+
<title lang="cn">Java并发编程实战</title>
7+
<author>Brian Goetz / Tim Peierls / Joshua Bloch / Joseph Bowbeer / David Holmes / Doug Lea</author>
8+
<year>2012</year>
9+
<price>69</price>
10+
</book>
11+
12+
<!-- 英文书本信息 -->
13+
<book>
14+
<title lang="en">Java Concurrency in Practice</title>
15+
<author>Brian Goetz / Tim Peierls / Joshua Bloch / Joseph Bowbeer / David Holmes / Doug Lea</author>
16+
<year>2006</year>
17+
<price>59.99</price>
18+
</book>
19+
20+
<book>
21+
<title lang="de">德文书</title>
22+
<author>德国</author>
23+
<year>2014</year>
24+
<price>50</price>
25+
</book>
26+
27+
<book>
28+
<title>没有lang属性</title>
29+
<author>试试</author>
30+
<year>2016</year>
31+
</book>
32+
33+
</bookstore>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package cn.aofeng.demo.xml;
2+
3+
import javax.xml.xpath.XPath;
4+
import javax.xml.xpath.XPathConstants;
5+
import javax.xml.xpath.XPathExpressionException;
6+
import javax.xml.xpath.XPathFactory;
7+
8+
import org.w3c.dom.Node;
9+
import org.w3c.dom.NodeList;
10+
import org.xml.sax.InputSource;
11+
12+
/**
13+
* XPath语法实践。
14+
*
15+
* @author <a href="mailto:[email protected]">聂勇</a>
16+
*/
17+
public class XPathDemo {
18+
19+
public static void main(String[] args) throws XPathExpressionException {
20+
InputSource ins = new InputSource(XPathDemo.class.getResourceAsStream("/cn/aofeng/demo/xml/BookStore.xml"));
21+
XPathFactory factory = XPathFactory.newInstance();
22+
XPath xpath = factory.newXPath();
23+
NodeList result = (NodeList) xpath.evaluate("//title[@lang='de']", ins, XPathConstants.NODESET);
24+
for (int i = 0; i < result.getLength(); i++) {
25+
Node node = result.item(i);
26+
StringBuilder buffer = new StringBuilder()
27+
.append("NodeName=").append(node.getNodeName()).append(", ")
28+
.append("NodeValue=").append(node.getNodeValue()).append(", ")
29+
.append("Text=").append(node.getTextContent());
30+
System.out.println(buffer.toString());
31+
}
32+
}
33+
34+
}

0 commit comments

Comments
 (0)