有人可以找到此代码的错误。无论我选择什么XPath,它总是返回节点
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true);
domFactory.setIgnoringComments(true);
domFactory.setIgnoringElementContentWhitespace(true);
DocumentBuilder builder = domFactory.newDocumentBuilder();
Document dDoc = builder.parse("P:/MyBooks.xml");
NodeList myNodes = (NodeList) xpath.evaluate("//Title", dDoc, XPathConstants.NODESET);
System.out.println(myNodes.getLength());
MyBookx.xml是:
<?xml version="1.0" encoding="UTF-8"?>
<Books>
<Title attrib="title1"/>
<Title attrib="title2"/>
<Title attrib="title3"/>
</Books>
答案 0 :(得分:2)