You need to iterate over every span
, so you can use the spread-dot operator:
xml.body.ul.li.findAll {it.@class == 'odd'}*.span.each {println it.text()}
Question
I'm trying to analyse an XML tree using XmlSlurper
and GPath, and the behaviour of the findAll
method confuses me.
Say, for example, that you have the following XML tree:
<html>
<body>
<ul>
<li class="odd"><span>Element 1</span></li>
<li class="even"><span>Element 2</span></li>
<li class="odd"><span>Element 3</span></li>
<li class="even"><span>Element 4</span></li>
<li class="odd"><span>Element 5</span></li>
</ul>
</body>
</html>
Assuming that xml
has been initialised through one of XmlSlurper
's parse
methods, the following code executes as one would expect:
// Prints:
// odd
// odd
// odd
xml.body.ul.li.findAll {it.@class == 'odd'}.@class.each {println it.text()}
On the other hand:
// Doesn't print anything.
xml.body.ul.li.findAll {it.@class == 'odd'}.span.each {println it.text()}
I'm struggling to understand why I can use the special @
property (as well as others, such as **
), but not 'normal' ones.
I've looked at the API code, and what confuses me even more is that the getProperty
implementation (found in GPathResult
) seems to support what I'm trying to do.
What am I missing?
Solution
You need to iterate over every span
, so you can use the spread-dot operator:
xml.body.ul.li.findAll {it.@class == 'odd'}*.span.each {println it.text()}