Generate xml element named 'properties' with StreamingMarkupBuilder

KrOma

New Member
I need to generate some xml that includes an element called "properties". When I attempt the following, I notice that groovy is trying to get the properties of my class, as evidenced by its call to \[code\]getSomething\[/code\]. It should instead only generate an element named "properties".\[code\]class XMLGen{ public String getSomething() { println "Got something" } public String genXml() { def myProps = ['a':1, 'b':2] def xml = new groovy.xml.StreamingMarkupBuilder().bind{ rootTag{ properties{ // ??? myProps.each{ prop -> property(key: prop.key, value: prop.value) } } } } return groovy.xml.XmlUtil.serialize(xml) }}println new XMLGen().genXml()\[/code\]Output:\[code\]Got something<?xml version="1.0" encoding="UTF-8"?><rootTag> <properties> <property key="a" value="http://stackoverflow.com/questions/15821599/1"/> <property key="b" value="http://stackoverflow.com/questions/15821599/2"/> </properties></rootTag>\[/code\]How would I explicitly "tell" groovy to pass the method call on line (marked ???) to the markup builder?(Asking this question and answering it myself as I couldn't find a solution for this anywhere, but eventually "guessed" the right answer)
 
Back
Top