在groovy中构建XML

时间:2013-08-29 08:26:46

标签: xml groovy soapui builder

使用来自SO和codehaus的许多帮助文章和案例我写了这样的东西:

import groovy.xml.MarkupBuilder
def EUIDroster = context.expand(here goes some data like '[123000,123001]' )

def writer = new StringWriter()
def builder = new MarkupBuilder(writer)

builder() {
    EUIDList(){
        for (e in EUIDroster){EUID(e)}
    }
}

EUIDList.println()

我的目标是创建一个结构如下的xml文件:

<EUID>123001</EUID>
<EUID>123002</EUID>

我做错了什么?

1 个答案:

答案 0 :(得分:2)

试试这个:

import groovy.xml.MarkupBuilder

def EUIDroster = [123000,123001]
def writer = new StringWriter()
new MarkupBuilder( writer ).EUIDList {
    EUIDroster.each { e ->
        EUID( e )
    }
}
println writer.toString()

那应该打印:

<EUIDList>
  <EUID>123000</EUID>
  <EUID>123001</EUID>
</EUIDList>