Groovy web console

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

xml to json

Published 3 months 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{it.name() == 'row'}.
   collect{ row ->
       row.collectEntries{[KEY: row.id.text(), VALUE:row.value.text()]}
   }]
println new groovy.json.JsonBuilder(map).toPrettyString()