Java – JDOM, XPath and namespace interaction

I find it frustrating to use XPath expressions to extract some elements from JDOM documents This is a sample XML document – I want to completely remove the itemcost element from the document, but I can't get an XPath expression to evaluate anything right now

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

I usually only use expressions like / / SRV: itemcost to identify these elements. These elements work normally in other documents, but here it will constantly return 0 nodes in the list This is 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);

Where response is the JDOM element containing the above XML fragment (verified with xmloutputter) When parsing this document, the node keeps having size () = = 0 Using the XPath parser in eclipse in the same document, this expression does not work After some digging, I asked the eclipse evaluator to use the following expression: / / * [local name() = 'itemcost'], but replacing / / SRV: itemcost with java code still didn't produce any results Another thing I noticed is that if I delete the namespace declaration from the XML, / / SRV: itemcost will parse correctly in the eclipse parser, but I can't delete it from the XML I can't figure it out now. I really appreciate some right directions

Thank you.

Edit: fixed code –

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

Solution

Strange, indeed... I tested it around JDOM, and your code fragment produced an empty list. The following works as expected:

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

It produces output:

//srv:ItemCost
2
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>