import java.io.File;
import java.io.FileInputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class FindElementsByAbsoluteLocationWithXPath {
public static void main(String[] args) throws Exception {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(false);
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new FileInputStream(new File("in.xml")));
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
String expression;
Node node;
NodeList nodeList;
// 1. root element
expression = "/*";
node = (Node) xpath.evaluate(expression, doc, XPathConstants.NODE);
System.out.println("1. " + node.getNodeName());
// 2. root element (by name)
expression = "/rss";
node = (Node) xpath.evaluate(expression, doc, XPathConstants.NODE);
System.out.println("2. " + node.getNodeName());
// 3. element under rss
expression = "/rss/channel";
node = (Node) xpath.evaluate(expression, doc, XPathConstants.NODE);
System.out.println("3. " + node.getNodeName());
// 4. all elements under rss/channel
expression = "/rss/channel/*";
nodeList = (NodeList) xpath.evaluate(expression, doc, XPathConstants.NODESET);
System.out.print("4. ");
for (int i = 0; i < nodeList.getLength(); i++) {
System.out.print(nodeList.item(i).getNodeName() + " ");
}
System.out.println();
// 5. all title elements in the document
expression = "//title";
nodeList = (NodeList) xpath.evaluate(expression, doc, XPathConstants.NODESET);
System.out.print("5. ");
for (int i = 0; i < nodeList.getLength(); i++) {
System.out.print(nodeList.item(i).getNodeName() + " ");
}
System.out.println();
// 6. all elements in the document except title
expression = "//*[name() != 'title']";
nodeList = (NodeList) xpath.evaluate(expression, doc, XPathConstants.NODESET);
System.out.print("6. ");
for (int i = 0; i < nodeList.getLength(); i++) {
System.out.print(nodeList.item(i).getNodeName() + " ");
}
System.out.println();
// 7. all elements with at least one child element
expression = "//*[*]";
nodeList = (NodeList) xpath.evaluate(expression, doc, XPathConstants.NODESET);
System.out.print("7. ");
for (int i = 0; i < nodeList.getLength(); i++) {
System.out.print(nodeList.item(i).getNodeName() + " ");
}
System.out.println();
// 8. all level-5 elements (the root being at level 1)
expression = "/*/*/*/*";
nodeList = (NodeList) xpath.evaluate(expression, doc, XPathConstants.NODESET);
System.out.print("8. ");
for (int i = 0; i < nodeList.getLength(); i++) {
System.out.print(nodeList.item(i).getNodeName() + " ");
}
System.out.println();
}
}
input:
Java Tutorials and Examples 2 en-us http://www.javacodegeeks.com/ http://examples.javacodegeeks.com/
输出:
eSiteGroup站群管理系统是基于eFramework低代码开发平台构建,是一款高度灵活、可扩展的智能化站群管理解决方案,全面支持SQL Server、SQLite、MySQL、Oracle等主流数据库,适配企业级高并发、轻量级本地化、云端分布式等多种部署场景。通过可视化建模与模块化设计,系统可实现多站点的快速搭建、跨平台协同管理及数据智能分析,满足政府、企业、教育机构等组织对多站点统一管控的
1. rss 2. rss 3. channel 4. title language item item 5. title title title 6. rss channel language item link item link 7. rss channel item item 8. title link title link










