Skip to content

Instantly share code, notes, and snippets.

Created March 15, 2011 12:29
Show Gist options
  • Save anonymous/870666 to your computer and use it in GitHub Desktop.
Save anonymous/870666 to your computer and use it in GitHub Desktop.
/**
* Created by IntelliJ IDEA.
* User: kyon
* Date: 11/03/15
* Time: 0:54
* To change this template use File | Settings | File Templates.
*/
class KeyValue{
def key
def value
KeyValue(key, value){
this.key = key
this.value = value
}
}
String convert(List list) {
StringWriter sw = new StringWriter()
def xml = new groovy.xml.MarkupBuilder(sw)
xml.langs(type: "current") {
list.each{
switch (it.value){
case String : "${it.key}"(it.value)
break
default : "${it.key}"(it.value)
}
}
}
return sw.toString();
}
list = [new KeyValue("key1","value1"),new KeyValue("key2","value2"),new KeyValue("key3", [new KeyValue("key3-1","value3-1"),new KeyValue("key3-2","value3-2")])]
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 expect.equals(convert(list))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment