Groovy web console

subscribe to the feed Subscribe
to this
site
kmb (via #groovywebconsole)
tweet this snippet Tweet
this
script

kmb

Published 8 months ago by kmb with tags xml parser iterator
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
def xmlString = '''
    <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>
'''

def parsedXml = new XmlSlurper().parseText(xmlString)

Iterator iterator = parsedXml.value.books.book.iterator()
int i = 1;
while(iterator.hasNext()) {
    Object obj = iterator.next();
    System.out.println(i + " " + obj + "\n")
    i++;
}