Given the following XML:
<list version="1.0">
<meta>...</meta>
<resources start="0" count="167">
<resource classname="Quote">
<field name="name">USD/KRW</field>
<field name="price">1024.400024</field>
<field name="symbol">KRW=X</field>
</resource>
...
</resources>
</list>
In order to find the right <resource>
and get it's price
I do the following:
def slurper = new XmlSlurper()
def result = slurper.parse(XML_URL)
def node = result.depthFirst().find { it.text() == "KRW=X" }
println node.parent().find { it['@name'] == "price" }.text()
However the result is that parent()
does not implement find(Closure)
which doesn't quite match with the docs: http://groovy.codehaus.org/gapi/groovy/util/slurpersupport/GPathResult.html
More strange node.parent().size()
returns 1 when I expect it to return 3 as per the above XML
My questions:
Is my code correct and why is not working?
Is this the shortest way of achieving the expected result?
Why
node.parent().size()
is returning 1 ? The same goes fornode.parent().parent().size()
,node.parent().parent().parent().size()
and so on...