Paul Frank Allan
Paul Frank Allan

Reputation: 741

How do I parameterize grails/groovy xml MarkupBuilder .builder syntax?

This is my first question on SO so be gentle. I am writing some groovy code to generate xml using MarkupBuilder. The problem is that I have to generate lots of similar xml for lots of different product types and the code will become huge if I cannot parameterize it. Showing you might help you understand better:

def writer = new StringWriter()

def builder = new groovy.xml.MarkupBuilder(writer)

builder.'cr:request'('xmlns:prodType': 'http://www.myurl/ProductType', 'xmlns:cr': 'http://www.myurl/customerRequest')
{

...
// Bla bla lots of elements and attributes
...

builder.'prodType:ProductGroupName'(ID:"IDPRD"+itemCount, internalID:internalID)
{

 productGroup("PGroup")

 productName("PName")

 ProductSpecificDetails()
 {
   param("paramA")

   stringValue("valA")

   param("paramB")

   stringValue("valB")

   ...

I am trying to parameterize 'prodType:ProductGroupName' or even just ProductGroupName in the code above. This means I will allow me to pass in various values for this along with the param list in order to generate xml dynamically for different products.

Looking online I have tried surrounding ProductGroupName with ${} and I also passing it as a map [:] but so far to no avail.

Does anyone know how I can achieve this?

Any help is much appreciated.

Thanks, Paul.

Upvotes: 0

Views: 480

Answers (1)

tim_yates
tim_yates

Reputation: 171084

Assuming you have a variable called productGroupName, you should be able to use (note the double quotes):

builder."prodType:$productGroupName"(ID:"IDPRD$itemCount", internalID:internalID)

Is that what you meant?

Upvotes: 1

Related Questions