0

I'm trying to loop over an expression like this changing the string 'question-hyperlink' to an item in a list I'm new to groovy and can't find a way that works. Do you really have to hard code this ? Every example I can find is hardcoded

.find{ it.@class == 'question-hyperlink'}it.book.title 
Jayan
  • 18,003
  • 15
  • 89
  • 143
user1741202
  • 121
  • 1
  • 1
  • 6

1 Answers1

3

No need to be hardcoded, you can use a list and you can use the in operator:

xml = '''<div>
    <div class="header">header div</div>
    <div class="body">body div</div>
    <span class="footer">footer span</span>
</div>
'''

node = new XmlSlurper().parseText xml

// the element's classes we want
classes = ['header', 'body']

contents = node.breadthFirst().findAll { it.@class in classes }*.text()

assert contents == ['header div', 'body div']
Will
  • 14,348
  • 1
  • 42
  • 44