※JSON
package script import groovy.json.JsonOutput import groovy.json.JsonSlurper def json = JsonOutput.toJson([1, 'a', 3.2, [a: 1]]) // 轉成 JSON 字串 println json // println JsonOutput.prettyPrint(json) // 印出格式化後的 JSON 字串 List<Object> a = new JsonSlurper().parse(json.getBytes()) // 將 JSON 字串轉成物件 println a class Animal { Integer id String name } def animalList = [ new Animal(id: 1, name: 'tiger'), new Animal(id: 2, name: 'turtle') ] def animalJson = JsonOutput.toJson(animalList) println animalJson // println JsonOutput.prettyPrint(animalJson) List<Animal> la = new JsonSlurper().parse(animalJson.getBytes()) println la
※
※XML
final def xml = '''<html> <head> <title>Test</title> </head> <body> <h1>YEAH</h1> <ul> <li class=\'aaa\'>AA</li> <li class=\'bbb\'>BB</li> <li class=\'ccc\'>CC</li> </ul> <ul> <li class=\'add\'>AD</li> <li class=\'aee\'>AE</li> <li class=\'aff\'>AF</li> </ul> </body> </html> '''
※
※
def rtn = new XmlSlurper().parseText(xml) // 根只能一個,也就是最外層的 html 只能有一個 println rtn.name() // html println rtn.body[0].h1[0].text() // YEAH println rtn.body.h1.text() // YEAH println rtn.body.ul.li[1].text() // B println rtn.body.ul.li[1]["@class"] // bbb println rtn.body.ul.li[1].'@class' // bbb println rtn.body.ul.li[1].@class // bbb println rtn.body.ul.li.text() // AABBCCADAEAF println rtn.body.ul.li.@class // aaabbbcccaddaeeaff def list = [] rtn.body.ul.each { ul -> ul.li.each { list << it } println() } println list // [AA, BB, CC, AD, AE, AF]
※取屬性有三種寫法
※depthFirst、children()
def filter = rtn.body.ul.depthFirst().findAll { it.@class in ['aaa', 'add'] }/*.collect { //如果想輸出非 text,可再加 collect it.@class }*/ println "filter= $filter" [AA, AD] def filter0 = rtn.body.ul.children().findAll { println it } println filter0 def filter1 = rtn.body.ul.'*'.find { println it.@class // aaa println it.name() // li it } println "filter1= $filter1" // AA def filter2 = rtn.body.ul.'**'.find { println it.@class // println it.name() // ul it } println "filter2= $filter2" // AABBCC def filter3 = rtn.body.ul.'*'.findAll { println it.@class // aaa\r\nli\r\nbbb\r\nli... println it.name() // li it } println "filter3= $filter3" // AABBCCADAEAF def filter4 = rtn.body.ul.'**'.findAll { println it.@class // println it.name() // ul\r\naaa\r\nli\r\nbbb... it } println "filter4= $filter4" // [AABBCC, AA, BB, CC, ADAEAF, AD, AE, AF]
※depthFirst() 可用 '**' 代替;children() 可用 '*' 代替
※產生 XML
import groovy.xml.MarkupBuilder //<aaa display="true"> // <bbb xxx="x" ppp="1">123</bbb> // <bbb xxx="o" ppp="2"> // 456 // <ddd>sub</ddd> // </bbb> // <ccc xxx="x" ppp="3">789</ccc> // <ccc xxx="o" ppp="4">012</ccc> //</aaa> def sw = new StringWriter() def xml = new MarkupBuilder(sw) xml.aaa(display: 'true') { bbb(xxx: 'x', ppp: '1', value: 123) bbb(xxx: 'o', ppp: '2', value: 456) { ddd(value: 'sub') } ccc(xxx: 'x', ppp: '3', value: 789) ccc(xxx: 'o', ppp: '4', value: '012') } println sw
※
※使用bean,產生XML
class Aaa { String display List<Bbb> b List<Ccc> c } class Bbb { String xxx String ppp String value Ddd d } class Ccc { String xxx String ppp String value } class Ddd { String value } def d = new Ddd(value: 'sub') def b1 = new Bbb(xxx: 'x', ppp: 1, value: 123) def b2 = new Bbb(xxx: 'o', ppp: 2, value: 456, d: d) def c1 = new Bbb(xxx: 'x', ppp: 3, value: 789) def c2 = new Bbb(xxx: 'o', ppp: 4, value: '012') def aaa = new Aaa(display: true, b: [b1, b2], c: [c1, c2]) def sw = new StringWriter() def xml = new MarkupBuilder(sw) xml.aaa(display: aaa.display) { for (def bc : aaa) { /* for (def bd : bc.b) { bbb(xxx: bd.xxx, ppp: bd.ppp, value: bd.value) { if (bd.d != null) { ddd(value: bd.d.value) } } } */ aaa.b.each { ab -> bbb(xxx: ab.xxx, ppp: ab.ppp, value: ab.value) { ab.d.each { ddd(value: it.value) } } } bc.c.each { ccc(xxx: it.xxx, ppp: it.ppp, value: it.value) } } } println sw
※註解是我一開始用閉包時,ddd 這一層包不進去 bbb,所以只好使用 for 迴圈的方式,後來發現 aaa.b 不用 it 才有辦法做到
※IO
假設已有一個檔案 xxx.txt,內容是:123
abc
456
def
※
def file = new File('xxx.txt') println file.getText() println file.readLines() // [123, abc, 456, def] println file.readLines('UTF-8') // [123, abc, 456, def] file.each { println it } file.eachLine { content, num -> println "$num $content" } println file.withReader { def buffer = new char[2] it.read(buffer) buffer } def out = new File('ooo.txt') out.withWriter { String enter = System.getProperty('line.separator') it.append('abc').append(enter).append('123').append(enter) it.append('def').append(enter).append('456') } def copyFile(File src, File dest) { if (!src.exists()) { src.createNewFile() } src.withReader { r -> def lines = r.readLines() dest.withWriter { w -> lines.eachWithIndex { String content, int i -> w.append("$i $content" + System.getProperty('line.separator')) } } } println 'copy success' } copyFile(new File('ooo.txt'), new File('copy.txt'))
※
沒有留言:
張貼留言