4

我在使用 XPath 表达式从 JDOM 文档中提取一些元素时遇到了非常令人沮丧的事情。这是一个示例 XML 文档 - 我想从文档中完全删除 ItemCost 元素,但目前我无法获取 XPath 表达式来评估任何内容。

  <srv:getPricebookByCompanyResponse xmlns:srv="http://ess.com/ws/srv">
     <srv:Pricebook>
        <srv:PricebookName>Demo Operator Pricebook</srv:PricebookName>
        <srv:PricebookItems>
           <srv:PricebookItem>
              <srv:ItemName>Demo Wifi</srv:ItemName>
              <srv:ProductCode>DemoWifi</srv:ProductCode>
              <srv:ItemPrice>15</srv:ItemPrice>
              <srv:ItemCost>10</srv:ItemCost>
           </srv:PricebookItem>
           <srv:PricebookItem>
              <srv:ItemName>1Mb DIA</srv:ItemName>
              <srv:ProductCode>Demo1MbDIA</srv:ProductCode>
              <srv:ItemPrice>20</srv:ItemPrice>
              <srv:ItemCost>15</srv:ItemCost>
           </srv:PricebookItem>
        </srv:PricebookItems>
     </srv:Pricebook>
  </srv:getPricebookByCompanyResponse>

我通常只会使用诸如 //srv:ItemCost 之类的表达式来识别这些元素,这在其他文档上工作得很好,但是在这里它会不断地返回列表中的 0 个节点。这是我一直在使用的代码:

Namespace ns = Namespace.getNamespace("srv","http://ess.com/ws/srv");   
XPath filterXpression = XPath.newInstance("//ItemCost");
filterXpression.addNamespace(ns);   
List nodes = filterXpression.selectNodes(response);

其中 response 是包含上述 XML 片段的 JDOM 元素(使用 XMLOutputter 验证)。每当解析此文档时,节点都会持续具有 size()==0。在 Eclipse 中对同一个文档使用 XPath 解析器,这个表达式也不起作用。经过一番挖掘,我让 Eclipse 评估器使用以下表达式://*[local-name() = 'ItemCost'],但是用它替换 Java 代码中的 //srv:ItemCost 仍然没有产生任何结果。我注意到的另一件事是,如果我从 XML 中删除名称空间声明, //srv:ItemCost 将在 Eclipse 解析器中正确解析,但我无法从 XML 中删除它。我现在一直在为我们在这个问题上的几个小时挠头,真的很感激朝着正确的方向轻推。

非常感谢

编辑:固定代码 -

Document build = new Document(response);
XPath filterXpression = XPath.newInstance("//srv:ItemCost");
List nodes = filterXpression.selectNodes(build);
4

1 回答 1

2

确实很奇怪...我在 jdom 方面进行了测试,您的代码段生成了一个空列表,以下工作按预期工作:

public static void main(String[] args) throws JDOMException, IOException {
    File xmlFile = new File("sample.xml");
    SAXBuilder builder = new SAXBuilder();
    Document build = builder.build(xmlFile);        
    XPath filterXpression = XPath.newInstance("//srv:ItemCost");
    System.out.println(filterXpression.getXPath());
    List nodes = filterXpression.selectNodes(build);        
    System.out.println(nodes.size());
}

它产生输出:

//srv:ItemCost
2
于 2010-07-05T19:15:12.957 回答