Skip to content

Instantly share code, notes, and snippets.

@uehaj
Forked from bluepapa32/keyValueXml4.groovy
Created March 17, 2011 12:07
Show Gist options
  • Save uehaj/874217 to your computer and use it in GitHub Desktop.
Save uehaj/874217 to your computer and use it in GitHub Desktop.
def kvmap = [
key1: "value1",
key2: "value2",
key3: [
"key3-1" : "value3-1",
"key3-2" : "value3-2",
],
]
LinkedHashMap.metaClass.toXml = { builder ->
delegate.each { k, v ->
builder."$k" {
v in String ? builder.mkp.yield(v) : v.toXml(builder)
}
}
}
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 new StringWriter().with {
new groovy.xml.MarkupBuilder(delegate).with {
doubleQuotes = true
langs(type: 'current') {
kvmap.toXml(delegate)
}
}
delegate.toString()
} == expect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment