The most simple way to create XML content in the standard output or file.
def fw = new FileWriter("/somepath/persons.xml" )
// to write to standard out, remove fw
def xml = new groovy.xml.MarkupBuilder(fw)
xml.content() {
xml.parameter(x:0)
xml.person(id:100){
firstname("John")
lastname("Long")
}
xml.person(id:101){
firstname("Ken")
lastname("Smith")
}
}
Result
<content> <parameter x='0' /> <person id='100'> <firstname>John</firstname> <lastname>Long</lastname> </person> <person id='101'> <firstname>Ken</firstname> <lastname>Smith</lastname> </person> </content>
Please note the difference between MarkupBuilder and StramingMarkupBuilder. More information about XML processing at groovy.codehaus.org
MarkupBuilder is not very flexible, but simple to use for some basic task. If you require comments, XML declarations and more, your rather use the StreamingMarkupBuilder.
Advertisement