Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy I can't take the content of a tag using XPath in a Java method?
    text
    copied!<p>I am very new in <strong>XPath</strong> and I have the following problem:</p> <p>I have a Java method that receives data from a webservices and these data are in a XML document, so I have to use XPath to take a specific value inside this XML result document.</p> <p>In particular I have that this is the entire XML output provided by my web service (the web service response):</p> <pre><code>&lt;s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"&gt; &lt;s:Body&gt; &lt;getConfigSettingsResponse xmlns="http://tempuri.org/"&gt; &lt;getConfigSettingsResult&gt;&lt;![CDATA[&lt;root&gt; &lt;status&gt; &lt;id&gt;0&lt;/id&gt; &lt;message&gt;&lt;/message&gt; &lt;/status&gt; &lt;drivers&gt; &lt;drive id="tokenId 11"&gt; &lt;shared-secret&gt;Shared 11&lt;/shared-secret&gt; &lt;encoding&gt;false&lt;/encoding&gt; &lt;compression /&gt; &lt;/drive&gt; &lt;drive id="tokenId 2 "&gt; &lt;shared-secret&gt;Shared 2 &lt;/shared-secret&gt; &lt;encoding&gt;false&lt;/encoding&gt; &lt;compression&gt;false&lt;/compression&gt; &lt;/drive&gt; &lt;/drivers&gt; &lt;/root&gt;]]&gt;&lt;/getConfigSettingsResult&gt; &lt;/getConfigSettingsResponse&gt; &lt;/s:Body&gt; &lt;/s:Envelope&gt; </code></pre> <p>Now in a Java class I perform the following operations:</p> <pre><code>XPath xPath; // An utility class for performing XPath calls on JDOM nodes Element objectElement; // An XML element //xPath = XPath.newInstance("s:Envelope/s:Body/getVersionResponse/getVersionResult"); try { // XPath selection: xPath = XPath.newInstance("s:Envelope/s:Body"); xPath.addNamespace("s", "http://schemas.xmlsoap.org/soap/envelope/"); objectElement = (Element) xPath.selectSingleNode(documentXML); if (objectElement != null) { result = objectElement.getValue(); System.out.println("RESULT:"); System.out.println(result); } } catch (JDOMException e) { // TODO Auto-generated catch block e.printStackTrace(); } </code></pre> <p>and the result of printing the content of the <strong>result</strong> variable is this output:</p> <pre><code>RESULT: &lt;root&gt; &lt;status&gt; &lt;id&gt;0&lt;/id&gt; &lt;message&gt;&lt;/message&gt; &lt;/status&gt; &lt;drivers&gt; &lt;drive id="tokenId 11"&gt; &lt;shared-secret&gt;Shared 11&lt;/shared-secret&gt; &lt;encoding&gt;false&lt;/encoding&gt; &lt;compression /&gt; &lt;/drive&gt; &lt;drive id="tokenId 2 "&gt; &lt;shared-secret&gt;Shared 2 &lt;/shared-secret&gt; &lt;encoding&gt;false&lt;/encoding&gt; &lt;compression&gt;false&lt;/compression&gt; &lt;/drive&gt; &lt;/drivers&gt; &lt;/root&gt; </code></pre> <p>Now my problem is that I want to access only ad the content of the <strong>0</strong> tag, so I want that (in this case) my <strong>result</strong> variable have to contain the <strong>0</strong> value.</p> <p>But I can't, I have try to change the previous XPath selection with:</p> <pre><code>xPath = XPath.newInstance("s:Envelope/s:Body/s:status/s:id"); </code></pre> <p>But doing in this way I obtain that my <strong>objectElement</strong> is <strong>null</strong></p> <p>Why? What am I missing? What have I to do to obtain that mu result variable contains the content of the <strong>id</strong> tag?</p> <p>Tnx</p> <p>Andrea</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload