Groovy web console

subscribe to the feed Subscribe
to this
site

Untitled

Published 3 months ago by Anonymous with tags XmlSlurper
Actions Execute script  ▶ Edit in console Back to console Show/hide line numbers View recent scripts
String books = """
<response>
  <books>
    <book available="20" id="1">
      <title>Don Quixote</title>
      <author id="1">Miguel de Cervantes</author>
    </book>
    <book available="14" id="2">
      <title>Catcher in the Rye</title>
      <author id="2">JD Salinger</author>
    </book>
    <book available="13" id="3">
      <title>Alice in Wonderland</title>
      <author id="3">Lewis Carroll</author>
    </book>
  </books>
</response>

"""

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

assert response.books.book.size() == 3
assert response.books.book[-1].title.text() == 'Alice in Wonderland' // check last book title