Groovy web console

subscribe to the feed Subscribe
to this
xico (via #groovywebconsole)
tweet this snippet Tweet


Published 1 year ago by xico with tags xml
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
final String books = '''
    <response version-api="2.0">
                <book available="20" id="1">
                    <title>Don Xijote</title>
                    <author id="1">Manuel De Cervantes</author>
                <book available="14" id="2">
                    <title>Catcher in the Rye</title>
                   <author id="2">JD Salinger</author>
               <book available="13" id="3">
                   <title>Alice in Wonderland</title>
                   <author id="3">Lewis Carroll</author>
               <book available="5" id="4">
                   <title>Don Xijote</title>
                   <author id="4">Manuel De Cervantes</author>

def response = new XmlSlurper().parseText(books)

def catcherInTheRye = response.value.books.'*'.find { node-> /* node.@id == 2 could be expressed as node['@id'] == 2 */ == 'book' && node.@id == '2'

print catcherInTheRye.title.text()