Skip to content

Instantly share code, notes, and snippets.

@tyuki39
Created March 16, 2011 14:01
Show Gist options
  • Save tyuki39/872539 to your computer and use it in GitHub Desktop.
Save tyuki39/872539 to your computer and use it in GitHub Desktop.
This gist is fork from https://gist.github.com/870666 by @kyon_mm
/**
* Created by IntelliJ IDEA.
* User: kyon
* Date: 11/03/15
* Time: 0:54
* To change this template use File | Settings | File Templates.
*/
String.metaClass.toXML = { builder, entry ->
builder."${entry.key}"(entry.value)
}
LinkedHashMap.metaClass.toXML = { builder, entry ->
builder."${entry.key}" {
entry.value.each { it.toXML(builder) }
}
}
LinkedHashMap$Entry.metaClass.toXML = { builder ->
delegate.value.toXML(builder, delegate)
}
def kvmap = [
key1: "value1",
key2: "value2",
key3: [
"key3-1" : "value3-1",
"key3-2" : "value3-2",
],
]
def sw = new StringWriter()
def xml = new groovy.xml.MarkupBuilder(sw)
xml.doubleQuotes = true // 属性はダブルクォートだよね!
xml.langs(type: 'current') {
kvmap.each {
it.toXML(xml)
}
}
def expect = '''\
<langs type="current">
<key1>value1</key1>
<key2>value2</key2>
<key3>
<key3-1>value3-1</key3-1>
<key3-2>value3-2</key3-2>
</key3>
</langs>'''
assert sw.toString() == expect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment