Groovy ----> XML
import groovy.xml.MarkupBuilder
def out = new StringWriter()
def xml = new MarkupBuilder(out)
def priceList = [ ' 1.64 ', ' 2.45', ' 3.85 ', ' 4.64 ', ' 5.23 ' ]
xml.product {
name(type: " 商品名 ","汽水-小七")
disc "300ML"
price(num: priceList.size()) {
for (p in priceList) {
price p
}
}
}
println out.toString()
#############结果Result##############
<product>
<name type=' 商品名 '>汽水-小七</name>
<disc>300ML</disc>
<price num='5'>
<price> 1.64 </price>
<price> 2.45</price>
<price> 3.85 </price>
<price> 4.64 </price>
<price> 5.23 </price>
</price>
</product>
如果你要在<product>里面加入ID 可以这样做 xml.product(id:i)
import groovy.xml.MarkupBuilder
def out = new StringWriter()
def xml = new MarkupBuilder(out)
def priceList = [ ' 1.64 ', ' 5.23 ' ]
println priceList.getClass().name
def i = '12 cans 7up'
xml.product(id:i) {
name(type: " soda ","小七")
disc "300ML"
price(num: priceList.size()) {
for (p in priceList) {
price p
}
}
}
println out.toString()
############################Result##########
<product id='12 cans 7up'>
<name type=' soda '>小七</name>
<disc>300ML</disc>
<price num='5'>
<price> 1.64 </price>
<price> 2.45</price>
<price> 3.85 </price>
<price> 4.64 </price>
<price> 5.23 </price>
</price>
</product>
Groovy --------->/** * Created by IntelliJ IDEA. * User: James * Date: Mar 18,2009 * Time: 4:05:08 PM * To change this template use File | Settings | File Templates. */import groovy.xml.MarkupBuilderdef out = new StringWriter()def html = new MarkupBuilder(out)//html.'${html}'html.html { body { font(color:'red',size:6) { b "Hello,world!" } }}println out.toString()#############结果Result##############<html> <body> <font color='red' size='6'> <b>Hello,world!</b> </font> </body></html>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。