groovy xml操作

1. groovy解析xml格式数据

groovy使用xmlSluper对xml进行解析,先定一个xml格式的字符串:

final String xml = '''
    <response version-api="2.0">
        <value>
            <books id="1" classification="android">
                <book available="20" id="1">
                    <title>疯狂Android讲义</title>
                    <author id="1">李刚</author>
                </book>
                <book available="14" id="2">
                   <title>第一行代码</title>
                   <author id="2">郭林</author>
               </book>
               <book available="13" id="3">
                   <title>Android开发艺术探索</title>
                   <author id="3">任玉刚</author>
               </book>
               <book available="5" id="4">
                   <title>Android源码设计模式</title>
                   <author id="4">何红辉</author>
               </book>
           </books>
           <books id="2" classification="web">
               <book available="10" id="1">
                   <title>Vue从入门到精通</title>
                   <author id="4">李刚</author>
               </book>
           </books>
       </value>
    </response>
'''

1.1 基本使用

下面是xml解析的一个例子,分别输出了节点和属性:

//开始解析此xml数据
def xmlSluper = new XmlSlurper()
def response = xmlSluper.parseText(xml)

println response.value.books[0].book[0].title.text()
println response.value.books[0].book[0].author.text()
println response.value.books[1].book[0].@available

输出结果

疯狂Android讲义
李刚
10

1.2 查询

查询所有作者是李刚的书名:

def xmlSluper = new XmlSlurper()
def response = xmlSluper.parseText(xml)

def list = []
response.value.books.each { books ->
    //下面开始对书结点进行遍历
    books.book.each { book ->
        def author = book.author.text()
        if (author.equals('李刚')) {
            list.add(book.title.text())
        }
    }
}
println list.toListString()

输出结果:

[疯狂Android讲义, Vue从入门到精通]

groovy为我们提供了一个深度遍历的方法,通过该方法一样可以实现上述功能。

def xmlSluper = new XmlSlurper()
def response = xmlSluper.parseText(xml)
//深度遍历xml数据
def titles = response.depthFirst().findAll { book ->
    return book.author.text() == '李刚' ? true : false
}
println titles.toListString()

深度遍历另一种写法是把depthFirst()使用’**’替换,效果是一样的。
使用广度遍历方法遍历xml数据:

def xmlSluper = new XmlSlurper()
def response = xmlSluper.parseText(xml)

//广度遍历xml数据
def name = response.value.books.children().findAll { node ->
    node.name() == 'book' && node.@id == '2'
}.collect { node ->
    return node.title.text()
}

println name

输出结果:

[第一行代码]

广度遍历children()方法可以使用’*’替代,效果一样。

2. groovy创建xml格式数据

groovy生成xml主要使用MarkupBuilder类,该类是生成xml的核心类。

2.1 基础使用

使用MarkupBuilder类生成xml的例子:

def sw = new StringWriter()
def xmlBuilder = new MarkupBuilder(sw) //用来生成xml数据的核心类
//根结点langs创建成功
xmlBuilder.langs(type: 'current', count: '3',
        mainstream: 'true') {
    //第一个language结点
    language(flavor: 'static', version: '1.5', 'java') {
        age('16')
    }
    language(flavor: 'dynamic', version: '1.6') {
        age('10')
    }
    language(flavor: 'dynamic', version: '1.9', 'JavaScript')
}

println sw

输出结果:

<langs type='current' count='3' mainstream='true'> 
    <language flavor='static' version='1.5'>java
        <age>16</age>
    </language>
    <language flavor='dynamic' version='1.6'>
        <age>10</age>
    </language>
    <language flavor='dynamic' version='1.9'>JavaScript</language>
</langs>

2.2 将实体类数据转换成xml格式数据

def sw = new StringWriter()
def xmlBuilder = new MarkupBuilder(sw) //用来生成xml数据的核心类
def langs = new Langs()
xmlBuilder.langs(type: langs.type, count: langs.count,
        mainstream: langs.mainstream) {
    //遍历所有的子结点
    langs.languages.each { lang ->
        language(flavor: lang.flavor,
                version: lang.version, lang.value)
    }
}
println sw
//对应xml中的langs结点
class Langs {
    String type = 'current'
    int count = 3
    boolean mainstream = true
    def languages = [
            new Language(flavor: 'static',
                    version: '1.5', value: 'Java'),
            new Language(flavor: 'dynamic',
                    version: '1.3', value: 'Groovy'),
            new Language(flavor: 'dynamic',
                    version: '1.6', value: 'JavaScript')
    ]
}
//对应xml中的languang结点
class Language {
    String flavor
    String version
    String value
}

输出结果:

<langs type='current' count='3' mainstream='true'>
    <language flavor='static' version='1.5'>Java</language>
    <language flavor='dynamic' version='1.3'>Groovy</language>
    <language flavor='dynamic' version='1.6'>JavaScript</language>
</langs>
发布了70 篇原创文章 · 获赞 16 · 访问量 13万+

猜你喜欢

转载自blog.csdn.net/qq282330332/article/details/89172604