Groovy MarkupBuilder node

Consider the following code:

def builder = new MarkupBuilder()
builder.root() {
}

I would like to delegate the creation of root children into a separate method. How can I complete this task? Some possible options for creating and returning a node from a method or passing a parent node and adding them to a method (both examples would be helpful).

+3
source share
1 answer

The Groovy website provides an explanation of how to achieve this.

Example:

def writer = new StringWriter()
def xml = new groovy.xml.MarkupBuilder(writer)
xml.books() {
   createBookNode(xml, 2, 'mrhaki')
}

def createBookNode(builder, repeat, username) {
    repeat.times {
       builder.person(name: username)
    }
}

println writer.toString()

The output will be:

<books>
    <person name="mrhaki"/>
    <person name="mrhaki"/>
</books>
+7
source

All Articles