/**
* 生成xml格式数据
* <langs type='current' count='3'>
* <language flavor='static' version='1.5'>Java</language>
* <language flavor='dynamic' version='1.6'>Groovy</language>
* <language flavor='dynamic' version='1.8'>javascript</language>
* </langs>
*/
def sw = new StringWriter()
def xmlBuilder = new MarkupBuilder(sw) //用来生成xml数据的核心类
//根结点langs创建成功
xmlBuilder.langs(type: 'current', count: '3') {
//第一个language结点
language(flavor: 'static', version: '1.5') { age(28)}
language(flavor: 'dynamic', version: '1.6', "Groovy")
language(flavor: 'dynamic', version: '1.8', "javascript")
}
println sw
String rs = '''
<langs type='current' count='3'>
<language flavor='static' version='1.5'>
<age>28</age>
</language>
<language flavor='dynamic' version='1.6'>Groovy</language>
<language flavor='dynamic' version='1.8'>javascript</language>
</langs>
'''
/**
* 生成xml格式数据
* <langs type='current' count='3'>
* <language flavor='static' version='1.5'>Java</language>
* <language flavor='dynamic' version='1.6'>Groovy</language>
* <language flavor='dynamic' version='1.8'>javascript</language>
* </langs>
*/
def sw = new StringWriter()
def xmlBuilder = new MarkupBuilder(sw) //用来生成xml数据的核心类
def langs = new Langs()
xmlBuilder.langs(type: langs.type, count: langs.count) {
//遍历所有子结点
langs.languages.each {
la -> language(flavor: la.flavor, version: la.flavor, la.value)
}
}
println sw
class Langs {
String type = 'current'
String count = '3'
def languages = [
new Language(flavor: 'static', version: '1.5', value: 'java'),
new Language(flavor: 'dynamic', version: '1.6', value: 'Groovy'),
new Language(flavor: 'dynamic', version: '1.8', value: 'javascript')
]
}
//对应xml的language节点
class Language {
String flavor
String version
String value
}
网友评论