Java内容重新整理删除过期的东西

This commit is contained in:
estom
2025-09-14 03:49:42 -04:00
parent 9b8524ff80
commit 885b795e45
413 changed files with 643 additions and 1340 deletions

View 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>

View 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());
}
}
}