首先申明下,本文为笔者学习《Groovy 程序设计》的笔记,并加入笔者自己的理解和归纳总结。
1. XmlParser解析XML
可以解析xml文件,也可以解析字符串。
def persons = new XmlParser().parse("persons.xml")
或者
xmlStr = """
<persons>
<person name="Mike" address="ShangHai">
<phone>131111111</phone>
<phone>131222222</phone>
</person>
<person name="Lily" address="BeiJing">
<phone>132111111</phone>
<phone>132222222</phone>
</person>
<person name="Jack" address="ShenZheng">
<phone>133111111</phone>
<phone>133222222</phone>
</person>
</persons>
"""
def persons = new XmlParser().parseText(xmlStr)
XmlParser
直接使用节点名称来访问,通过@+属性名
来访问节点属性值,使用text
方法来访问内容值。
println persons.person.@name // [Mike, Lily, Jack]
println persons.person[1].@name // Lily
println persons.person[0].phone.collect{it.text()} // [131111111, 131222222]
2. XmlSlurper解析XML
XmlSlurper
的用法和XmlParser
的调用相似。
def persons = new XmlSlurper().parseText(xmlStr)
println persons.person.@name // [Mike, Lily, Jack]
println persons.person[1].@name // Lily
println persons.person[0].phone.collect{it.text()} // [131111111, 131222222]
XmlSlurper
解析命名空间
xmlStr = """
<persons xmlns:app="app">
<app:person name="Mike" address="ShangHai">
<phone>131111111</phone>
<phone>131222222</phone>
</app:person>
<person name="Lily" address="BeiJing">
<phone>132111111</phone>
<phone>132222222</phone>
</person>
<app:person name="Jack" address="ShenZheng">
<phone>133111111</phone>
<phone>133222222</phone>
</app:person>
</persons>
"""
def persons = new XmlSlurper().parseText(xmlStr).declareNamespace(x:'app')
println persons.person[1].@name // Lily
println persons.'x:person'[1].@name // Jack
3. MarkupBuilder构建适合中小型文档
writer = new StringWriter()
mb = new groovy.xml.MarkupBuilder(writer)
mb.persons {
person(name:"Mike", address:"ShangHai") {
phone(131111111)
phone(131222222)
}
person(name:"Lily", address:"BeiJing") {
phone(132111111)
phone(132222222)
}
person(name:"Jack", address:"ShengZhen") {
phone(133111111)
phone(133222222)
}
}
println writer
返回
<persons>
<person name='Mike' address='ShangHai'>
<phone>131111111</phone>
<phone>131222222</phone>
</person>
<person name='Lily' address='BeiJing'>
<phone>132111111</phone>
<phone>132222222</phone>
</person>
<person name='Jack' address='ShengZhen'>
<phone>133111111</phone>
<phone>133222222</phone>
</person>
</persons>
4. StreamingMarkupBuilder构建适合大型文档
xmlDoc = new groovy.xml.StreamingMarkupBuilder().bind {
mkp.xmlDeclaration() // <?xml version='1.0'?>
mkp.declareNamespace(app:"APP") // 命名空间
persons {
comment << "Just comment" // 注释
app.person(name:"Mike", address:"ShangHai") {
phone(131111111)
phone(131222222)
}
app.person(name:"Lily", address:"BeiJing") {
phone(132111111)
phone(132222222)
}
person(name:"Jack", address:"ShengZhen") {
phone(133111111)
phone(133222222)
}
}
}
println xmlDoc
返回
<?xml version='1.0'?>
<persons xmlns:app='APP'>
<!--Just comment-->
<app:person name='Mike' address='ShangHai'>
<phone>131111111</phone>
<phone>131222222</phone>
</app:person>
<app:person name='Lily' address='BeiJing'>
<phone>132111111</phone>
<phone>132222222</phone>
</app:person>
<person name='Jack' address='ShengZhen'>
<phone>133111111</phone>
<phone>133222222</phone>
</person>
</persons>