【发布时间】:2009-03-16 17:00:08
【问题描述】:
我正在使用以下代码使用从流中获取的 XPath 查询一些 XML。
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(false);
DocumentBuilder builder = domFactory.newDocumentBuilder();
Document doc = builder.parse(inputStream);
inputStream.close();
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
XPathExpression expr = xpath.compile("//FOO_ELEMENT");
Object result = expr.evaluate(doc, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++) {
System.out.println(nodes.item(i).getNodeValue());
我已经通过将流转换为字符串来检查流中的内容 - 它就在那里 - 所以它不像流中没有数据。
现在这让我很烦 - 因为我尝试了各种不同的代码,但我仍然在“System.out.println”行打印'null' - 我在这里错过了什么?
注意:我想查看元素内的文本。
【问题讨论】:
-
你的意思是System.out.println(nodes.item(i).getNodeValue());打印“null”或者你得到一个 NullPointerException?
-
现在对其进行了编辑以使其更清晰 - Eddie 欢呼。