ibaralf
ibaralf

Reputation: 12528

Grails: Creating XML nodes with variables

I can't seem to figure out how to create nodes that are not hardcoded. Example:

def aval = "someValue" 
def xml = new MarkupBuilder()

xml.outTag(attr: aval)   // outTag is hardcoded

My problem is: what if I don't know the name of the node (like in my example, what if I want the outTag to be a variable). I'm using this when I read a directory/files and create an XML from it. Thanks.

Upvotes: 2

Views: 2745

Answers (1)

OverZealous
OverZealous

Reputation: 39570

Use this format:

import groovy.xml.MarkupBuilder

def dynamicTag = "blah"
def aval = "someValue"
def xml = new MarkupBuilder()

xml."$dynamicTag"(attr: aval)

This worked for me (Groovy 1.8)

Upvotes: 7

Related Questions