Created
May 5, 2012 15:21
-
-
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.
This file contains hidden or 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
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