Groovy web console

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

xml to json

Published 3 weeks ago by nmrao with tags xml json
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
def xml = """<data>     <row>       <id>USA</id>       <value>United  States of America</value>    </row>    <row>        <id>CAN</id>       <value>Canada</value>     </row> </data>"""
def parsed = new XmlSlurper().parseText(xml)
def map = [(parsed[0].name): parsed.'**'.
   findAll{ == 'row'}.
   collect{ row ->
       row.collectEntries{[KEY:, VALUE:row.value.text()]}
println new groovy.json.JsonBuilder(map).toPrettyString()