Skip to content

Instantly share code, notes, and snippets.

@bluepapa32
Forked from tyuki39/keyValueXml4.groovy
Created March 16, 2011 14:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bluepapa32/872612 to your computer and use it in GitHub Desktop.
Save bluepapa32/872612 to your computer and use it in GitHub Desktop.
Map -> XML 変換
def kvmap = [
key1: "value1",
key2: "value2",
key3: [
"key3-1" : "value3-1",
"key3-2" : "value3-2",
],
]
sw = new StringWriter()
xml = new groovy.xml.MarkupBuilder(sw);
xml.doubleQuotes = true
xml.langs(type: 'current') { visit kvmap }
def visit(Map m) { m.each { k, v -> xml."$k" { visit v } } }
def visit(String s) { xml.mkp.yield s }
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