首页 文章

Xpath从错误的节点返回值

提问于
浏览
0

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

这是我 class 的构造函数:

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 回答

  • 3

    试试这段代码

    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());
                    }
                }
            }
    }
    
  • 0

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

    .//name
    

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

    name
    

相关问题