Skip to content

Instantly share code, notes, and snippets.

@uehaj
Created March 18, 2011 04:48
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uehaj/875631 to your computer and use it in GitHub Desktop.
Save uehaj/875631 to your computer and use it in GitHub Desktop.
convert map to xml
def kvmap = [
key1: "value1",
key2: "value2",
key3: [
"key3-1" : "value3-1",
"key3-2" : "value3-2",
],
]
sw = new StringWriter()
new groovy.xml.MarkupBuilder(sw).with {
doubleQuotes = true
visitor = { k, v ->
"$k" { v instanceof Map ? v.collect(visitor) : mkp.yield(v) }
}
langs(type: 'current') { kvmap.collect visitor }
}
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
@uehaj
Copy link
Author

uehaj commented Mar 20, 2011

collectはeachでも良い(3文字短縮)。つーかgrepでもfindAllでも良い。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment