< Scala
Scala makes processing and producing XML very easy. This enables programmers to very easily talk to web services and other sources of XML data.
XML Literals
In Scala, XML can be used as a literal. This means you can do something like this:
val doc = <person living="true"><name>Jimbo Wales</name><job>Supreme Overlord of Wikiland</job></person>
You can include expressions within XML using curly braces like this:
class Person {
// ...
def toXML() = <person><name>{this.name}</name><job>{this.job}</job></person>
}
As you can see curly braces are special characters in Scala XML, therefore if you just want to display a curly brace just repeat it twice: {{
Exploring XML
When you have some XML, you can explore it using the backslash and double backslash methods:
val doc = <person living="true"><name>Jimbo Wales</name><job>Supreme Overlord of Wikiland</job></person>
println((doc \ "name").text) // prints "Jimbo Wales"
println(doc.attributes("living")) // prints "true"
println(doc \\ "@living") // finds all the attributes named living and in this case prints true
This article is issued from Wikibooks. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.