import
groovy.xml.MarkupBuilder
def out = new StringWriter()
def xml = new MarkupBuilder(out)
def friendList = [ ' Tony ' , ' Alan ' , ' Leona ' , ' Cloudy ' , ' terry ' ]
xml.person {
name(type: " 网名 " , " 山风小子 " )
address " 上海 "
friends(num: friendList.size()) {
for (f in friendList) {
friend f
}
}
}
<name type='网名'>山风小子</name>
<address>上海</address>
<friends num='5'>
<friend>Tony</friend>
<friend>Alan</friend>
<friend>Leona</friend>
<friend>Cloudy</friend>
<friend>terry</friend>
</friends>
def out = new StringWriter()
def xml = new MarkupBuilder(out)
def friendList = [ ' Tony ' , ' Alan ' , ' Leona ' , ' Cloudy ' , ' terry ' ]
xml.person {
name(type: " 网名 " , " 山风小子 " )
address " 上海 "
friends(num: friendList.size()) {
for (f in friendList) {
friend f
}
}
}
println out.toString()
输出结果:
<name type='网名'>山风小子</name>
<address>上海</address>
<friends num='5'>
<friend>Tony</friend>
<friend>Alan</friend>
<friend>Leona</friend>
<friend>Cloudy</friend>
<friend>terry</friend>
</friends>
</person>
用了groovy.xml.MarkupBuilder,它是Groovy builder家族的一员,常用它来生成XML文件。