Skip to content

Instantly share code, notes, and snippets.

@kmizu
Created May 5, 2012 15:21
Show Gist options
  • Save kmizu/2603258 to your computer and use it in GitHub Desktop.
Save kmizu/2603258 to your computer and use it in GitHub Desktop.
An application of SIP-11 - String interpolation and formatting. This enables XML enables to make XML element using arbitrary XML parser.
import scala.xml._
object XMLStringLiteral extends App {
implicit class RichStringContext(self: StringContext) {
def xml(params: Any*): Elem = {
val Seq(first, rest@_*) = self.parts
val source = first + (params, rest).zipped.map{_+_}.mkString
XML.loadString(source)
}
}
val p = xml"""<person>
<name>Kota Mizushima</name>
<age>${27 + 1}</age>
</person>
"""
println((p \\ "name").text)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment