展开全部
使用 XPath。e69da5e6ba9062616964757a686964616f31333332633631try (final FileReader reader = new FileReader("xpathexample.xml")){
XPath xpath = XPathFactory.newInstance().newXPath();
String expression = "count(//content)";
InputSource source = new InputSource(reader);
Number count = (Number) xpath.evaluate(expression, source, XPathConstants.NUMBER);
System.out.println(count.intValue());
} catch (final IOException | XPathExpressionException e) {
System.err.println(e.getMessage());
}
dom4j 可以使用 Document的selectSingleNode 返回null的就是没有val node = doc.selectSingleNode("//PrimaryKey/Key/@R")
node: org.dom4j.Node = nullval node = doc.selectSingleNode("//PrimaryKey/Key/@Ref")
node: org.dom4j.Node = org.dom4j.tree.DefaultAttribute@151d52e [Attribute: name Ref value "o30"]