2

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.

0

1 Answer 1

7

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)

3
  • Thanks. That worked. For some weird reason, I could not find this is any tutorial. I appreciate the help.
    – ibaralf
    Sep 6, 2011 at 22:07
  • Great! Since this worked, please mark this response as "Accepted"! Sep 7, 2011 at 1:40
  • The reason why it works is that you can call any method with the dynamic notation object."$methodName" where methodName is a string variable with the name of the method as value
    – Uberto
    Jul 5, 2012 at 14:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.