Groovy语法学习(六)JSON、XML使用

groovy对于json和xml的生成和解析不需要额外导入库了,直接调用api使用。

一、JSON

(一) JSON字符串创建
1.使用JsonBuilder类

JsonBuilder有call()方法传入closure,所以可以接像下面这样写,相当于调用call方法。

def builder = new JsonBuilder()
builder{
    name'jabamiYu'
    age 12
}

assert builder.toString()=="{\"name\":\"jabamiYu\",\"age\":12}"

2.使用对象直接转字符串
class Franxx{
    def name
    def sex

}
def p =new Franxx(name:"zero two",sex:"female")
println new JsonBuilder(p)
println JsonOutput.toJson(p)
println JsonOutput.prettyPrint(JsonOutput.toJson(p))

结果:

{"sex":"female","name":"zero two"}
{"sex":"female","name":"zero two"}
{
    "sex": "female",
    "name": "zero two"
}

更多稍微复杂的用法,大家可以去参考对应几个类注释,注释上都有例子。

二、XML

(一) 解析xml

使用安卓的Manifest文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example.ty">

    <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">

        <meta-data android:name="hello" android:value="1"/>
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>

</manifest>

解析

def parser = new XmlParser().parse(new File("AndroidManifest.xml"))

//创建命名空间

def ns = new Namespace('http://schemas.android.com/apk/res/android','android')

Node nodeactivity = parser.application[0].activity[0]

println nodeactivity.attributes()[ns.name]

//获得application节点

Node node = parser.application[0]

//NodeList

Node meta = node.'meta-data'[0]

node.remove(meta)

node.appendNode('meta-data',[(ns.name):'a',(ns.value):'b',(ns.hh):'hh'])

new XmlNodePrinter(new PrintWriter(new File("replace.xml"))).print(parser)

replace.xml

<manifest package="com.example.ty">
  <application android:allowBackup="true" xmlns:android="http://schemas.android.com/apk/res/android" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
      <intent-filter>
        <action android:name="android.intent.action.MAIN"/>
        <category android:name="android.intent.category.LAUNCHER"/>
      </intent-filter>
    </activity>
    <meta-data android:name="a" android:value="b" android:hh="hh"/>
  </application>
</manifest>
(二) 生成xml


def fw = new FileWriter(new File("normal.xml"))
def builder = new MarkupBuilder(fw)
builder.html{
    mkp.comment("test")
    head("hello",m:'a'){
        title("com.jabamiYu")
    }
    body{

    }
}

def sb = new StreamingMarkupBuilder()
sb.encoding = 'UTF-8'
def closure  = {
    mkp.xmlDeclaration()
    html{
        head(id:1){

        }
    }
}
def sw = sb.bind(closure)
println  sw.toString()

结果:
normal.xml文件

<html><!-- test -->
  <head m='a'>hello
    <title>com.jabamiYu</title>
  </head>
  <body />
</html>

打印:

<?xml version='1.0' encoding='UTF-8'?>
<html><head id='1'></head></html>

只总结了一些简单用法,更加复杂的用法同样可以通过查看注释。

猜你喜欢

转载自blog.csdn.net/a568478312/article/details/80014488