O'Reilly logo

Pragmatic Scala by Venkat Subramaniam

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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> ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required