0

I have the XML and I want to find the path to price tag and code

  <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Body>
     <getProductDetailsResponse xmlns="http://warehouse.example.com/ws">
       <getProductDetailsResult>
         <productID>12345</productID>
         <productName>Стакан граненый</productName>
         <description>Стакан граненый. 250 мл.</description>
         <price>9.95</price>
         <currency>
             <code>840</code>
             <alpha3>USD</alpha3>
             <sign>$</sign>
             <name>US dollar</name>
             <accuracy>2</accuracy>
         </currency>
         <inStock>true</inStock>
       </getProductDetailsResult>
     </getProductDetailsResponse>
   </soap:Body>
</soap:Envelope>

I can get the path to values if I delete namespace-xmlns="http://warehouse.example.com/ws"... The path is - /soap:Envelope/soap:Body/getProductDetailsResponse/getProductDetailsResult/price/text()

But server return it only with namespaces.

How I can get values?

Thanks in Advance

Aydin
  • 65
  • 1
  • 10
  • Sadly, this another example of why SOAP is overkill. You'll need to add a namespace in your XPATH query. The way to do this differs based on how you're running your XPath (php, C#, java, xsl). What are you using to parse your SOAP XML? – William Walseth Dec 19 '15 at 13:54
  • @WilliamWalseth I try to add namespace to XPath, but it doesn't work. I parse xml in PL/SQL (Oracle) – Aydin Dec 19 '15 at 14:01
  • Something like this article explains how. After you read it you'll appreciate why SOAP is overkill. https://community.oracle.com/thread/2553686?start=0&tstart=0 – William Walseth Dec 19 '15 at 14:13
  • If you can apply an XSL transformation in PL/SQL run the transformation in this article, and then you can use regular XPath, without namespaces. http://stackoverflow.com/questions/5268182/how-to-remove-namespaces-from-xml-using-xslt – William Walseth Dec 19 '15 at 14:15
  • @WilliamWalseth Thanks William, for yuor suggestions. I also answer to this question by myself. – Aydin Dec 19 '15 at 17:05

2 Answers2

4

You can get all values from the XML.

For example, if you want to get the <price> tag value use

/soap:Envelope/soap:Body/*/*/*[4]/text()

Or code tag - use

/soap:Envelope/soap:Body/*/*/*/*[1]/text()
Mr Lister
  • 45,515
  • 15
  • 108
  • 150
Aydin
  • 65
  • 1
  • 10
0

If you use java, you must manage prefix:

String expression="your path here !"

XPath xpath = XPathFactory.newInstance().newXPath();
NamespaceContext ctx = new NamespaceContext() {
    public String getNamespaceURI(String prefix) {
        if (prefix == null) throw new NullPointerException("Null prefix");
        return XMLConstants.XML_NS_URI; //   .NULL_NS_URI;
    }
    public String getPrefix(String uri) {
        throw new UnsupportedOperationException();
    }
    public Iterator getPrefixes(String uri) {
        throw new UnsupportedOperationException();
    }
};
xpath.setNamespaceContext(ctx);
XPathExpression expr = xpath.compile(expression) ; 

with

expression="/Envelope/Body/getProductDetailsResponse/getProductDetailsResult/price";

and

expression="/Envelope/Body/getProductDetailsResponse/getProductDetailsResult/currency/code";

it works, with that:

NodeList nodes  = (NodeList) expr.evaluate(document, XPathConstants.NODESET);

  for (int k = 0; k < nodes.getLength(); k ++) {
      Node nodeSegment = nodes.item(k);
      if (nodeSegment.getNodeType() == Node.ELEMENT_NODE) {
          Element eElement = (Element) nodeSegment;
          System.out.println("TAG="+eElement.getTagName());              
          System.out.println("TEXT CONTENT="+eElement.getTextContent());
      }

you can get other solutions about prefix: NamespaceContext and using namespaces with XPath

Community
  • 1
  • 1