Xpath从错误的节点返回值

时间:2011-06-07 22:16:00

标签: java xml xpath

我在使用XPath时遇到了一些麻烦。由于某种未知的原因,我从表达式得到的结果是来自另一个函数运行的结果。

这是我班级的构造函数:

Wine(Node ndWine){
    try{
     xpath = XPathFactory.newInstance().newXPath();
    }
    catch(Exception e)
    {}

    Node ndName = null;

    try{
        ndName = (Node)xpath.evaluate("//name", ndWine, XPathConstants.NODE);
    }
    catch(Exception e)
    {}
    if (ndName != null)
        name = ndName.getTextContent();
}

这是XML:

<cellar>
  <wine>
    <name>Jasnières</name>
  </wine>
  <wine>
    <name>Ballet d'Octobre</name>
  </wine>
</cellar>

在调用方法中,我有另一个xpath表达式,它将文档分解为<wine>元素列表。为每个节点调用上面的代码 在调试器中,我检查在第二次运行时ndWine节点实际上包含来自文档的第二个节点的数据,但评估总是返回Jasnieres值而不是ballet d'octobre,我无法理解。

任何根本原因的想法?

2 个答案:

答案 0 :(得分:3)

使用//启动XPath表达式使其成为绝对路径。即使您传入<wine>元素,它也会忽略它并从文档根开始。在前面添加.以使其成为相对路径:

.//name

或者更好的是,如果可以,请避免使用//语法。如果您确切知道descendant-or-self元素的确切位置,最好避免进行完整的<name>搜索。如果它们总是直接位于<wine>元素内,则使用此XPath:

name

答案 1 :(得分:0)

试试这段代码

try {                
     expr = xpath.compile("/cellar/wine/name");
     nodeList = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
} catch (XPathExpressionException ignored) {}

for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if (node != null) {
            NodeList childNodes = node.getChildNodes();
            for (int j = 0; j < childNodes.getLength(); j++) {
                Node childNode = childNodes.item(j);
                if (childNode.getNodeType() == Node.TEXT_NODE) {
                    System.out.println(childNode.getNodeValue());
                }
            }
        }
}
相关问题