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

edit

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

edit

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