mirror of
https://github.com/Estom/notes.git
synced 2026-02-05 19:43:57 +08:00
Java内容重新整理删除过期的东西
This commit is contained in:
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