Skip to content

Instantly share code, notes, and snippets.

@fumokmm
Created March 19, 2011 10:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fumokmm/877373 to your computer and use it in GitHub Desktop.
Save fumokmm/877373 to your computer and use it in GitHub Desktop.
/*
* 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