Groovy创建XML

时间:2014-09-21 00:07:08

标签: xml groovy

如何在Groovy中创建XML?

def xml = new groovy.xml.MarkupBuilder()
xml.langs(type:"current"){
  language("Java")
  language("Groovy")
  language("JavaScript")
}

是的,我知道,它有效.. 但我需要这样的XML:

<root>
  <one param="var">
    <some param="var">variable</some>
    <some param="var">variable</some>
    ...
  </one>
  <two>
    <some param="var">variable</some>
    <some param="var">variable</some>
    <some param="var">variable</some>
    ...
  </two>
  ...
</root>

我将循环循环。 Inner for和outer for,...但是如何将其添加到XML?

谢谢!

1 个答案:

答案 0 :(得分:2)

这样的事情开头?

def outerLoop = ['one', 'two', 'three', 'four']
def innerLoop = ['a', 'b']

def xml = new groovy.xml.MarkupBuilder()

xml.root {
    outerLoop.each { outer ->
        xml."$outer"(param: 'var') {
            innerLoop.each { inner ->
                xml."$inner"(params: 'abc', "value")
            }
        }
    }
}