Skip to content

Instantly share code, notes, and snippets.

@amitbhadra
Created February 22, 2018 08:32
Show Gist options
  • Save amitbhadra/af413251f12ca23522b8ad19021f8535 to your computer and use it in GitHub Desktop.
Save amitbhadra/af413251f12ca23522b8ad19021f8535 to your computer and use it in GitHub Desktop.
Parse a XML string in groovy using XmlSlurper
books = '''
<response version-api="2.0">
<value>
<books>
<book available="20" id="1">
<title>Don Xijote</title>
<author id="1">Manuel 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>
<book available="5" id="4">
<title>Don Xijote</title>
<author id="4">Manuel De Cervantes</author>
</book>
</books>
</value>
</response>
'''
response = new XmlSlurper().parseText(books)
response.value.books.book.each{
bookname = it.title
println bookname
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment