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>