You could do something like this; Wrap the node string into a { -> }
and evaluate it as a Closure, then set the delegate and call the closure:
import groovy.xml.*
def nodes = '''MyTag( attr:'help me' ) {
| AnotherTag( 'Help me!' )
|}'''.stripMargin()
println XmlUtil.serialize( new StreamingMarkupBuilder().bind {
Project {
c = Eval.me( "{ -> $nodes }" )
c.delegate = delegate
c()
}
} )
Which prints:
<?xml version="1.0" encoding="UTF-8"?><Project>
<MyTag attr="help me">
<AnotherTag>Help me!</AnotherTag>
</MyTag>
</Project>
However, you must be careful, as if that nodes
String comes from outside your system, it can be used to execute any code that is put in it.
If you're getting the nodes in a String, why not get them to write XML instead and save you a job? ;-)