Scala XML examples: XML literals, mixing XML and Scala source code, XPath searching

A really terrific feature about Scala is that XML handling is built into the language. This means you don't have to deal with XML as String objects, you deal with it as XML objects.

Here are just a few examples of using XML in Scala. First, you can create an XML literal like this:

scala> val hello = <p>Hello, world</p>
hello: scala.xml.Elem = <p>Hello, world</p>

Again, note that this is not a String, there are no double quotes; we've just defined an XML literal in Scala.

You can take this further and define a multiline XML literal like this:

scala> val foo = <p>Lorem ipsum
     |   something, something else,
     |   et cetera, et cetera</p>
foo: scala.xml.Elem = 
<p>Lorem ipsum
  something, something else,
  et cetera, et cetera</p>

As you can see, both of those XML literals are created as the type scala.xml.Elem. (I'll write more about this in future tutorials.)

Embedding Scala code in XML

To really blow your mind, you can embed Scala code in XML, as shown when I generate this UL/LI list:

scala> val fruits = List("apple", "banana", "orange")
fruits: List[java.lang.String] = List(apple, banana, orange)

scala> val ul = <ul>{ => <li>{i}</li>)}</ul>
ul: scala.xml.Elem = <ul><li>apple</li><li>banana</li><li>orange</li></ul>

scala> println(ul)

As you can see from that second line of code, I've embedded a Scala object (fruits) inside an XML literal; called the map method on that object; and used XML and a variable substitution inside the XML of my anonymous function; and assigned all of this to a val variable(!). You might be used to some of this in HTML/XML-specific templating tools, but to embed this within the Scala programming language makes it incredibly powerful.

Searching XML in Scala

You can also search XML in Scala. Using my last example, here's how you can search for all of the "<li>" elements in an XML object:

scala> ul \ "li"
res1: scala.xml.NodeSeq = NodeSeq(<li>apple</li>, <li>banana</li>, <li>orange</li>)

If you want to know how many <li> tags were found in the XML, you can just evaluate the length of that expression:

scala> (ul \ "li").length
res2: Int = 3

If you want to get the text from the <li> elements, you can get it like this:

scala> (ul \ "li").map(_.text)
res3: scala.collection.immutable.Seq[String] = List(apple, banana, orange)

More Scala and XML, coming soon

I hope these simple Scala XML examples have been helpful. We've seen how to:

  • Define a simple XML literal.
  • Define a multiline XML literal.
  • Weave XML and Scala source code together.
  • How to search XML for specific elements; find the number of results our search yielded; and how to extract the text from the elements we searched for.

On a related note, I've also written this Scala XML parsing, searching, XMLNS namespaces, and XPath tutorial.

I'll write much more about Scala and XML in the future, but for today, reporting live from Boulder, Colorado, this is Alvin Alexander.

Add new comment

The content of this field is kept private and will not be shown publicly.

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.