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:

val​ xmlFragment =
<symbol ticker=​"AAPL"​><units>200</units></symbol>
<symbol ticker=​"IBM"​><units>215</units></symbol>

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:

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

Get Pragmatic Scala now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.