XPath:
dom4j支持XPath的jar包.
jaxen-1.1-beta-6.jar
dom4j的XPath支持的API:
List document.selectNodes(String xPath);
Node document.selectSingleNode(String xPath);
代码:
@Test
/**
* DOM4J的XPath的写法:
*/
public void demo2() throws Exception{
// 创建解析器:
SAXReader reader = new SAXReader();
// 解析XML返回Document对象.
Document document = reader.read("xml/demo1.xml");
/*List<Node> list = document.selectNodes("//name");
for (Node node : list) {
Element element = (Element) node;
System.out.println(element.getText());
}*/
List<Node> list = document.selectNodes("//person['@id']");
for (Node node : list) {
Element element = (Element) node;
System.out.println(element.attributeValue("id"));
}
}