Gradle自动化构建(六) Groovy json、xml

json

import groovy.json.JsonOutput
import groovy.json.JsonSlurper
import part4.cls.Oop

// 对象转换为json
def list = [new Oop(name: 'John', age: 23), new Oop(name: 'Major', age: 44)]
String json = JsonOutput.toJson(list) // 将list转换为json,有一系列重载的该函数
println json // [{"age":23,"name":"John"},{"age":44,"name":"Major"}]
println JsonOutput.prettyPrint(json) // JsonOutput.prettyPrint(json) 将json转换成展开的形式,打印

// json转换为对象
def jsonSlurper = new JsonSlurper()
LinkedList<Oop> ls = jsonSlurper.parseText(json) // 将json字符串转换为对象(会自动转换数据类型),还有很多 parse 重载的函数,参看源码
println ls[0].name // John

###xml
import groovy.util.slurpersupport.GPathResult
import groovy.xml.MarkupBuilder

final String xml = '''
    <response version-api="2.0">
        <value>
            <books id="1" classification="android">
                <book available="20" id="1">
                    <title>android go</title>
                    <author id="1">松木</author>
                </book>
                <book available="14" id="2">
                   <title>android fine</title>
                   <author id="2">么为</author>
               </book>
           </books>
           <books id="2" classification="c++">
               <book available="10" id="1">
                   <title>c++ nice</title>
                   <author id="4">松木</author>
               </book>
           </books>
       </value>
    </response>
'''
// 解析xml
def xmlSlurper = new XmlSlurper() // 也可以用 XmlParser 类解析
GPathResult res = xmlSlurper.parseText(xml) // 同json一样,还有很多parse的重载函数, res 为 xml 的根节点 response
println res.value.books[0].book[0].title.text() // android go , text() 表示取 <title> 的内容
println res.value.books[0].book[0].title.name() // title , name() 表示取 <title> 的标签名
println res.value.books[1].book[0].@available // 10 , 获取 <book> 标签的属性 available 的值, @ 表示取属性值
// 查找所有 松木 的book
def list = []
res.value.books.each {
    books -> books.book.each {
        book ->
            def auth = book.author.text()
            if ('松木'.equals(auth)) {
                list.add(book.title.text())
            }
    }
}
println list // [android go, c++ nice]
// 深度遍历xml , depthFirst() 可以使用 '**' 代替
println res.depthFirst().findAll { book -> '松木'.equals(book.author.text()) } // [android go松木, c++ nice松木]
// 广度遍历xml , children() 可以使用 '*' 代替
def bs = res.value.books.children().findAll {
    node -> node.name()=='book' && node.@id=='1'
}.collect {
    node -> node.title.text()
}
println bs // [android go, c++ nice]

// 生成xml
def sw = new StringWriter()
def xmlBuilder = new MarkupBuilder(sw) // MarkupBuilder 用来生成xml , 生成的 xml 会写入 sw 参数中
xmlBuilder.langs(type: 'current', count: '3') { // MarkupBuilder 中并没有 langs , 这里的 langs 表示生成的xml的根节点,type、count 表示根节点的属性
    language(flavor: 'static', version: '1.5', 'Java') // 根节点下创建 <language> 标签, flavor: 表示属性, 如果不 : 表示标签的值,如 'Java'
    language(flavor: 'dynamic', version: '1.6', 'Groovy')
    language(flavor: 'auto', version: '14', 'C++') { } // 要给 language 添加子节点,给 language 添加闭包即可
}
/**
 <langs type='current' count='3'>
     <language flavor='static' version='1.5'>Java</language>
     <language flavor='dynamic' version='1.6'>Groovy</language>
     <language flavor='auto' version='14'>C++</language>
 </langs>
 */
println sw // 打印

// 用对象生成 xml
def stringWriter = new StringWriter()
def markupBuilder = new MarkupBuilder(stringWriter)
class Langs {
    String type = 'current'
    int count = 3
    def languages = [
            new Language(flavor: 'static',
                    version: '1.5', value: 'Java'),
            new Language(flavor: 'dynamic',
                    version: '1.3', value: 'Groovy'),
            new Language(flavor: 'auto',
                    version: '14', value: 'C++')
    ]
}
//对应xml中的languang结点
class Language {
    String flavor
    String version
    String value
}
def langs = new Langs()
markupBuilder.langs(type: langs.type, count: langs.count) { // langs 表示根节点, () 中表示根节点的属性
    //遍历所有的子结点
    langs.languages.each {
        lang -> language(flavor: lang.flavor, version: lang.version, lang.value) // language() 表示子节点
    }
}
/**
 <langs type='current' count='3'>
     <language flavor='static' version='1.5'>Java</language>
     <language flavor='dynamic' version='1.3'>Groovy</language>
     <language flavor='auto' version='14'>C++</language>
 </langs>
*/
println stringWriter // 打印
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值