Skip to content

Instantly share code, notes, and snippets.

@nobeans
Created March 16, 2011 15:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nobeans/872667 to your computer and use it in GitHub Desktop.
Save nobeans/872667 to your computer and use it in GitHub Desktop.
class XmlConvertCategory {
static String toXml(Map input) {
def sw = new StringWriter()
def builder = new groovy.xml.MarkupBuilder(sw)
builder.doubleQuotes = true
builder.metaClass.recursive = { Map map ->
map.each { key, value ->
"${key}"(value in Map ? { recursive(value) } : value)
}
}
builder.langs(type: 'current') {
recursive input
}
sw.toString()
}
}
def input = [
key1: "value1",
key2: "value2",
key3: [
"key3-1": "value3-1",
"key3-2": "value3-2",
]
]
def expected = '''\
<langs type="current">
<key1>value1</key1>
<key2>value2</key2>
<key3>
<key3-1>value3-1</key3-1>
<key3-2>value3-2</key3-2>
</key3>
</langs>'''
use(XmlConvertCategory) {
def result = input.toXml()
assert result == expected
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment