Created
March 15, 2011 12:29
-
-
Save anonymous/870666 to your computer and use it in GitHub Desktop.
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
/** | |
* 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