Create Dynamic XML using Groovy & parameterize the arguments
I would like to create a dynamic xml where some of the nodes and their sub nodes repeat based on the number of times I want them to and then the values of each node elements need to be parameterized from an external file.
an example of what I am trying to do is as below :
In the xml below I have some common elements(ex : <day> which does not repeat in the xml and I can have a single property for that and send values from an external file using a simple groovy.
The <food> tag set needs to repeat based on my preference(If I want it to be 10 then it should create 10 such nodes.
I dont mind creating multiple columns in a csv file for each value of the food tag set & reading using groovy.
<?xml version="1.0" encoding="UTF-8"?>
<breakfast_menu>
<day>${Properties#day}</day> //Will parameterize this in Properies tab and call from Groovy
<time>08:00:00</time> //Remains static
<food>
<name>${Properties#name1}</name> //Needs Parameterization
<price>${Properties#price1}</price> //Needs Parameterization
<description>
Two of our famous Belgian Waffles with plenty of real maple syrup
</description> //Needs Parameterization
<calories>650</calories> //Needs Parameterization
</food>
<food>
<name>${Properties#name2}</name>
<price>${Properties#price2}</price>
<description>
Light Belgian waffles covered with strawberries and whipped cream
</description>
<calories>900</calories>
</food>
<food>
<name>Berry-Berry Belgian Waffles</name>
<price>$8.95</price>
<description>
Belgian waffles covered with assorted fresh berries and whipped cream
</description>
<calories>900</calories>
</food>
</breakfast_menu>
Can someone please guide me on how this xml generation & data parameterization be done
Thanks
A
Here is how you can do it using Groovy.
Since you have more data, you can put it in a text file as shown below:
name1, price1, description1, calories1 name2, price2, description2, calories2
And the Script goes here:
//Make sure you pass the data.txt with absolute path in unix style def lines = new File('data.txt').readLines() def builder = new StreamingMarkupBuilder() builder.encoding = 'UTF-8' def xml = builder.bind { mkp.xmlDeclaration() //Use below line if you need name space as needed, remove otherwise namespaces << [ns:'http://example/break/fast'] breakfast_menu { day (new Date().format('EEE MMM d yyyy')) time( '08:00:00' ) lines.each { line -> def data = line.split(',')*.trim() food { name (data[0]) price (data[1]) description (data[2]) calories (data[3]) } } } } def updatedXml = groovy.xml.XmlUtil.serialize(xml)
log.info updatedXmlOutput
<?xml version="1.0" encoding="UTF-8"?> <breakfast_menu xmlns:ns="http://example/break/fast"> <day>Thu Apr 27 2017</day> <time>08:00:00</time> <food> <name>name1</name> <price>price1</price> <description>description1</description> <calories>calories1</calories> </food> <food> <name>name2</name> <price>price2</price> <description>description2</description> <calories>calories2</calories> </food> </breakfast_menu>
Ignore the first row. Haven't tried, Can you give a try ?
lines.eachWithIndex { line, index -> if (index) { def data = line.split(',')*.trim() food { name (data[0]) price (data[1]) description (data[2]) calories (data[3]) } } }