Created
March 19, 2011 10:19
-
-
Save fumokmm/877373 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: fumokmm | |
* Date: 11/03/19 | |
* Time: 16:09 | |
*/ | |
package jp.scaladojo | |
import scala.xml._ | |
class KeyValue(key: String, value: Any) { | |
val this.key: String = key | |
val this.value: Any = value | |
override def toString(): String = toXMLString | |
def toXMLString: String = { | |
val inner: String = this.value match { | |
case lst: List[KeyValue] => (for(kv <- lst) yield kv.toXMLString).mkString | |
case s : String => s | |
} | |
startTag + inner + endTag | |
} | |
def startTag = "<" + this.key + ">" | |
def endTag = "</" + this.key + ">" | |
} | |
object KeyValue { | |
def main(args: Array[String]): Unit = { | |
val keyValue = new KeyValue("langs", List( | |
new KeyValue("key1", "value1"), | |
new KeyValue("key2", "value2"), | |
new KeyValue("key3", List( | |
new KeyValue("key3-1", "value3-1"), | |
new KeyValue("key3-2", "value3-2") | |
)) | |
)) | |
val resultXml = XML.loadString(keyValue.toXMLString) | |
// Confirmation | |
assert(resultXml == <langs><key1>value1</key1><key2>value2</key2><key3><key3-1>value3-1</key3-1><key3-2>value3-2</key3-2></key3></langs>) | |
println(resultXml) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment