Saturday 10 May 2008

The Scala programming language and XML

The Scala programming language is a combined scripting and "proper" language, that sits on top of the Java VM. You can either run scripts similar to how you run a Ruby or Perl script, or compile your Scala classes to Java bytecode. You run a Scala application similar to how you run a Java application. You can also run a Scala application using the Java VM (but you have to add the Scala library jar file to your class path). You can mix Java and Scala programs, calling Scala objects from Java, and vice versa.

Scala has a feature that I have never seen in a language suitable for general programming: XML (processing) as a feature of the language. The people behind Scala has added XML to the syntax of the language itself. You do not have to load some library or use some special API for processing XML, since it's already part of the language.

It is not only that XML is valid in Scala code, but XML has its own built-in data types. For instance,

val xml = <vegetable>potato</vegetable>
is a valid Scala statement. In other words, XML-elements written in a Scala program are just not merely strings. The xml object can now be manipulated in various ways, much like a DOM object in Java (but with less hassle than in Java).

You can refer to variables in your XML:
val veg = "potato"
val col = "white"
val xml = <vegetable colour={col}>{veg}</vegetable>

// The value of xml now corresponds to
// <vegetable colour="white">potato</vegetable>



You can also embed function/method calls into XML elements. Imagine that you have a method that returns a sequence of n XML elements, like this (you'll need to import scala.xml.NodeSeq and scala.xml.NodeBuffer):

def genNumElems(n :Int) :NodeSeq = {
val result = new NodeBuffer
for(i <- 1 to n) {
result &+ (<number value={i.toString}/>)
}
result
}
(The odd-looking &+ operator means "add".)

You can now embed a call to genNumElems in an XML element, e.g., like this:
val numList = <number_list>{genNumElems(4)}<number_list/>

Printing numList produces:
<number_list>
<number value="1"></number>
<number value="2"></number>
<number value="3"></number>
<number value="4"></number>
</number_list>


If you want nicer output, you can use a PrettyPrinter (that you import from scala.xml._):
val pp = new PrettyPrinter(100,2) // width and indentation
println(pp.format(xml))


Reading and writing XML data/files to and from Scala is easy. The following is a one-liner that reads an XML file given as a command line argument (args(0)) and returns a list of all elements named "tr" that are child elements to any elements called "table" of the XML file:
val trNodes = scala.xml.XML.loadFile(args(0)) \\ "table" \ "tr"
You may print the <tr> elements (with an empty line between each element) thus:
trNodes.foreach(tr => println(tr + "\n"))

The built-in XML support in Scala's syntax and basic libraries are not the most important or interesting features of Scala, but they sure seem to be very useful.

(Incidentally, the table and tr elements above are present in Oocalc's (OpenOffice.org) XML format for spreadsheets.)

Update: It appears that is not always good advice to use scala.xml.XML.loadFile to read an XML document. One reason is that comment elements are lost. For more advanced XML processing, one should turn to scala.xml.parsing.ConstructingParser.fromFile.

Update: You may run into trouble when processing larger XML documents using the second approach. See this comment.

6 comments:

Unknown said...

Microsoft C# / Linq does pretty much the same... So Scala isn't the only statically typed language to support this....

Nikolaj Lindberg said...

Being a somewhat narrow-minded Unix/Linux type of person, I haven't looked at C#. Thanks for pointing this out!

Anonymous said...

I'm not a C# programmer, but:

This is what I've found on some blog.


"C# developers will be sad to hear that C# 3.0 won’t get XML literals"

So maybe Scala isn't one language to do it (VB.NET does it), but it makes is a pleasure (xpath using \\, etc).

Unknown said...

There's also ActionScript 3.0 - that's had XML literals for ages, and has much wider usage. Plus, despite the name, it isn't interpreted.

Anonymous said...

If performance or memory usage is important for XML tasks, check out vtd-xml

http://vtd-xml.sf.net

Nikolaj Lindberg said...

Hello Anonymous,

thanks for the tip. Someone else (I cannot remember in what context) suggested VTD-XML too. Maybe it's time to give it a try.

(Blogger (?) seems to have done something odd with the URL in the above comment, so you'll have to copy and paste it rather than click it.)