Created
March 18, 2011 04:48
-
-
Save uehaj/875631 to your computer and use it in GitHub Desktop.
convert map to xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
collectはeachでも良い(3文字短縮)。つーかgrepでもfindAllでも良い。