XML as a First-Class Citizen

Scala treats XML as a first-class citizen. So, instead of embedding XML documents into strings, you can place them inline in your code like you’d place an int or a Double value. Let’s take a look at an example:

UsingScala/UseXML.scala
 
val​ xmlFragment =
 
<symbols>
 
<symbol ticker=​"AAPL"​><units>200</units></symbol>
 
<symbol ticker=​"IBM"​><units>215</units></symbol>
 
</symbols>
 
 
println(xmlFragment)
 
println(xmlFragment.getClass)

We created a val named xmlFragment and directly assigned it to a sample XML content. Scala parsed the XML content and happily created an instance of scala.xml.Elem, as you see in the output:

 
<symbols>
 
<symbol ticker="AAPL"><units>200</units></symbol>
 
<symbol ticker="IBM"><units>215</units></symbol> ...

Get Pragmatic Scala now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.