如何参数化grails / groovy xml MarkupBuilder .builder语法?

时间:2023-01-04 19:41:34

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:

这是我关于SO的第一个问题,所以要温柔。我正在编写一些groovy代码来使用MarkupBuilder生成xml。问题是我必须为许多不同的产品类型生成许多类似的xml,如果我无法对它进行参数化,代码就会变得很大。显示您可能会帮助您更好地理解:

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.

我试图在上面的代码中参数化'prodType:ProductGroupName'或甚至只是ProductGroupName。这意味着我将允许我传递各种值以及param列表,以便为不同的产品动态生成xml。

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

在线查看我尝试使用$ {}包围ProductGroupName,我也将其作为地图[:]传递,但到目前为止无济于事。

Does anyone know how I can achieve this?

有谁知道我怎么能做到这一点?

Any help is much appreciated.

任何帮助深表感谢。

Thanks, Paul.

谢谢,保罗。

1 个解决方案

#1


1  

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

假设你有一个名为productGroupName的变量,你应该可以使用(注意双引号):

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

Is that what you meant?

这是你的意思吗?

#1


1  

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

假设你有一个名为productGroupName的变量,你应该可以使用(注意双引号):

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

Is that what you meant?

这是你的意思吗?