tags:

views:

68

answers:

2

I'm working with an API that returns a groovy.util.Node, allowing me to customize its generated XML. I need to append a child element into the Node, and I'm wondering if I can use MarkupBuilder syntax to modify the Node.

For example, here's something that works, but seems klunky:

withXml { rootNode ->
    def appendedNode = new Node(rootNode, 'foo', [name:'bar'])
    def appendedNodeChild = new Node(appendedNode, 'child', [blah:'baz'])
}

Is there a way to append to the rootNode using MarkupBuilder-ish syntax? Thanks.

A: 
new MarkupBuilder().root {
   foo( name:'bar' ) {
     child( blah:'blaz' )
   }
 }

don't know if I understand you question completely, but I believe you can do something like what is above

also this example from the documentation shows how you can use yield to add in additional content

http://groovy.codehaus.org/api/groovy/xml/MarkupBuilder.html

Aaron Saunders
Thanks, but the API I'm working with gives me a Node, and I'm not seeing a way to convert to/from a MarkupBuilder.
Dan Tanner
A: 

You can use groovy.util.Node's appendNode method:

withXml { rootNode ->
    rootNode.appendNode ('foo', [name: 'bar']).appendNode ('child', [blah: 'baz'])
}

The above code snippet will add add

<foo name="bar">
    <child blah="baz"/>
</foo>

to the rootNode.

Rolf Suurd