Create a simple XML document.
Our data is two single valued elements and a table of information represented as and array or arrays.
Our JSON file looks like:
{
"START_TIME" : "2012-084T15:02:23",
"STOP_TIME": "2012-084T21:13:44",
"RECORDS": [
[ "Uplink Carrier Phase Table", "00", "C132,0", "182", "22268" ] ,
[ "Downlink Carrier Phase Table", "01", "C133", "4052776", "378", "18605" ] ,
[ "Uplink Sequential Ranging Phase Table", "02", "C132", "11085466", "214", "16571" ] ,
[ "Downlink Sequential Ranging Phase Table", "03", "C133", "14631660", "324", "32" ] ,
[ "Sequential Ranging Table", "07", "C134", "14642028", "350", "32" ] ,
[ "Ramps Table", "09", "C132", "14653228", "144", "76" ] ,
[ "DRVID Table", "11", "C134", "14664172", "202", "32" ] ,
[ "Carrier Observable Table", "16", "C134", "14670636", "220", "307" ] ,
[ "Total Phase Table", "17", "C134", "14738176", "236", "307" ]
]
}
The RECORDS element is an array of arrays which is one way to store a table of information in JSON.
We will store this in a file called example-1.json.
Our velocity template looks like:
<?xml version="1.0" encoding="utf-8" standalone="yes"?> <doc> <start>$json.START_TIME</start> <stop>$json.STOP_TIME</stop> <description>Hello from JSON test</description> #foreach ($record in $json.RECORDS) <record> <first>$record.get(0)</first> #foreach ($field in $record) <field>$field</field> #end </record> #end </doc>
We will store this in a file called example-1.vm.
The template uses #foreach to process the each record and
a second #foreach to process each element in a record. The template also
demonstrates how to access a specific element using the .get() method for arrays.
Instructs igpp.docgen to parse the file "example-1.json" and place the results in a context named "json". Since the extension on "example-1.json" is ".json" igpp.docgen will parse the file JSON data, creating a parameter for each element.
Running this command will generate a plain text that looks like:
<?xml version="1.0" encoding="UTF-8"?> <doc> <start>2012-084T15:02:23</start> <stop>2012-084T21:13:44</stop> <description>Hello from JSON test</description> <record> <first>Uplink Carrier Phase Table</first> <field>Uplink Carrier Phase Table</field> <field>00</field> <field>C132,0</field> <field>182</field> <field>22268</field> </record> <!-- more records in actual output --> </doc>