JDOM,XPath和命名空间的交互 [英] JDOM, XPath and Namespace Interactions

查看:92
本文介绍了JDOM,XPath和命名空间的交互的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用XPath表达式从JDOM文档中提取某些元素的时间非常令人沮丧.这是一个示例XML文档-我想从文档中完全删除ItemCost元素,但是目前我很难让XPath表达式对任何内容求值.

I'm having a very frustrating time extracting some elements from a JDOM document using an XPath expression. Here's a sample XML document - I'd like to remove the ItemCost elements from the document altogether, but I'm having trouble getting an XPath expression to evaluate to anything at the moment.

  <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这样的表达式来标识这些元素,该表达式在其他文档上也可以正常工作,但是在这里它会连续返回List中的0个节点.这是我一直在使用的代码:

I would normally just use an expression such as //srv:ItemCost to identify these elements, which works fine on other documents, however here it continually returns 0 nodes in the List. Here's the code I've been using:

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

其中的响应是一个包含上述XML代码段(已通过XMLOutputter验证)的JDOM元素.解析此文档时,节点连续具有size()== 0.在同一文档上的Eclipse中使用XPath解析器,此表达式也不起作用.经过一番挖掘之后,我使Eclipse评估程序可以使用以下表达式://* [local-name()='ItemCost'],但是用此代码替换Java代码中的//srv:ItemCost仍然不会产生任何结果.我注意到的另一件事是,如果我从XML中删除了名称空间声明,//srv:ItemCost将在Eclipse解析器中正确解析,但是我无法从XML中删除它.我一直在努力工作,在这个时间里,我真的很感激在正确方向上的一些尝试.

Where response is a JDOM element containing the above XML snippet (verified with an XMLOutputter). nodes continually has size()==0 whenever parsing this document. Using the XPath parser in Eclipse on the same document, this expression does not work either. After some digging, I got the Eclipse evaluator to work with the following expression: //*[local-name() = 'ItemCost'], however replacing the //srv:ItemCost in the Java code with this still produced no results. Another thing I noticed is if I remove the namespace declaration from the XML, //srv:ItemCost will resolve correctly in the Eclipse parser, but I can't remove it from the XML. I've been scratching my head for ours hours on this one now, and would really appreciate some nudging in the right direction.

非常感谢

固定代码-

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

推荐答案

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

Strange, indeed... I tested on my side with jdom, and your snippet produced an empty list, the following works as intended:

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

这篇关于JDOM,XPath和命名空间的交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆