Convert JSON to XML using groovy? -


i have json file, in json file convert xml format using parser, , write xml file

i can't find examples of how in groovy

if have json looking this:

{ name: "sampleconfiguration", description: "sampledesc" version: "1.0", parameters: [     {         name: "sampleparameter",         description: "parameter description",         value: "20",         enabled: "1"     },     {         name: "items",         description: "parameter subparameters",         value:[             {                 name: "item",                 description: "nested parameter",                 value: "13"             },             {                 name: "item",                 description: "nested parameter 2",                 value: "test"             }         ]     } ]} 

then should convert xml looking this:

<?xml version="1.0"?> <sampleconfiguration version="1.0" description="sampledesc"> <params>     <sampleparameter enabled="1" description="parameter description">20</sampleparameter>     <items description="parameter subparameters">         <item description="nested parameter">13</item>         <item description="nested parameter 2">test</item>     </items> </params> </sampleconfiguration> 

i have been looking json xml converting code

if make json valid (" round names, , comma in initial block), can convert (specifically crafted example)

def json = ''' {     "name": "sampleconfiguration",     "description": "sampledesc",     "version": "1.0",     "parameters": [     {         "name": "sampleparameter",         "description": "parameter description",         "value": "20",         "enabled": "1"     },     {         "name": "items",         "description": "parameter subparameters",         "value":[             {                 "name": "item",                 "description": "nested parameter",                 "value": "13"             },             {                 "name": "item",                 "description": "nested parameter 2",                 "value": "test"             }         ]     } ]}'''  import groovy.json.* import groovy.xml.*  def xml = new jsonslurper().parsetext(json).with { j ->     new stringwriter().with { sw ->         new markupbuilder(sw)."$name"(version: version, description:description) {             params {                 parameters.each { p ->                     if(p.value instanceof list) {                         "$p.name"(description:p.description) {                             p.value.each { v ->                                 "$v.name"(description: v.description, v.value)                             }                         }                     }                     else {                         "$p.name"(description:p.description, p.value)                     }                 }             }         }         sw.tostring()     } }  println xml 

there no general case converting xml json know of.

the output example is:

<sampleconfiguration version='1.0' description='sampledesc'>   <params>     <sampleparameter description='parameter description'>20</sampleparameter>     <items description='parameter subparameters'>       <item description='nested parameter'>13</item>       <item description='nested parameter 2'>test</item>     </items>   </params> </sampleconfiguration> 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -