Note that there are some explanatory texts on larger screens.

plurals
  1. POFinding XML element from RESTClient with XMLSlurper
    text
    copied!<p>I'm writing a Spock test, in which I have a REST web service that returns an XML like this:</p> <pre><code>&lt;templates&gt; &lt;userTemplate id="1109"&gt; &lt;settingsXml/&gt; &lt;type&gt;USER&lt;/type&gt; &lt;label&gt;template111&lt;/label&gt; &lt;description&gt;template111&lt;/description&gt; &lt;/userTemplate&gt; &lt;userTemplate id="1141" isAutomaticTemplate="true"&gt; &lt;settingsXml/&gt; &lt;type&gt;USER&lt;/type&gt; &lt;label&gt;An updated user template&lt;/label&gt; &lt;/userTemplate&gt; &lt;/templates&gt; </code></pre> <p>My test want to verify that a particular userTemplate it is <strong>not</strong> in this document. So, using HTTP Builder's REST client and XMLSlurper, I'm doing the following:</p> <pre><code> res = settingsService.get(path: "templates") res.status == 200 def delTemplate = res.data.userTemplate.find { println it.@id == newUserTemplateId it.@id == newUserTemplateId } delTemplate </code></pre> <p>I would have thought that <em>delTemplate</em> would be null after calling <strong>find</strong> (because there's no template with that id; the expresion println it.@id == newUserTemplateId always prints <em>false</em>, in this case the value of <em>newUserTemplateId</em> is 1171).<br> However, <em>delTemplate</em> is of type groovy.util.slurpersupport.NoChildren, and it seems to contain a userTemplate element.</p> <p>Funny thing is if I write a quick script with the same XML as text (as oppossed of reading it from REST), <code>res.userTemplate.find { it.@id == 1171 }</code> returns null as expected.</p> <p>What am I doing wrong, or how could I solve this?</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