Groovy --> XML

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 ' ,  ' 2.45' ,  ' 3.85 ' ,  ' 4.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.MarkupBuilder

def 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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值