坚持使用XPath

时间:2010-07-06 15:57:56

标签: java xml xpath

有人可以找到此代码的错误。无论我选择什么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>

1 个答案:

答案 0 :(得分:2)