Scala and XML. The New Kid on the Block

development_8

Every time you turn around it seems there is a new computer language vying for the coveted spot as the Next Big Thing.  It can be hard to assess which is worth your attention and how these languages will make enough of a difference to warrant spending the time to learn how to do things you already know in your favorite programming language.  There is also the effort involved in grappling with all of the new concepts and, sometimes, exotic syntax of newer languages, all while learning the host of companion libraries and tools with overly cute names.  Having said that, one of the languages that has been on the rise in recent years is Scala and it might be worth considering all the time investment required to start using it.

Scala is a general purpose language which, unlike other newer languages like Swift, Go and Hack, did not spring out of tech giants like Google, Facebook and Apple.  It emerged from of the halls of academia.  It was created by Professor of Programming Martin Odersky, whose previous credits include work on the javac compiler for Java.  

I first came across Scala while working with Apache Solr.  Several tutorials and examples online related to Solr seemed to gush about how Scala could do something Java could do in far fewer lines of code.  I noted the enthusiasm, but did not pay much attention until I saw an example of using Scala with XML.  

Scala utilizes the concept of XML literals. XML does not have to be sourced from a string or from a file and run through any special methods or classes to be understood by Scala.  Scala natively recognizes XML allowing you to declare a chunk of XML just as easily as you would a string.  I can create an XML fragment with the following line:

 


val docFragment = <basketball><teams><team>Boston Celtics</team><team>New York Knicks</team><team>Memphis Grizzlies</team></teams></basketball>

 

Note the lack of quotes or special handlers.  This line of code creates a NodeSeq object.  Applying XPath queries on this NodeSeq is just as simple.  In Scala, to get to nested descendants at any level, you use the \\ operator.  If you wanted to loop through the individual team nodes then you could use the following line:

 


for (team <- docFragment \\ "team" ) …

 

After using the Java XML parsers for a few years, which seemed to require at least five objects and 20 lines of code to get anything interesting to happen, I started turning to scripting language like PHP to handle XML Fragments and node manipulation.  Still, using the DOM extension in PHP, the equivalent code for the &quot;basketball&quot; XML fragment still wears out its welcome:


$doc = new DomDocument();

$docFragment = $dom.createDocumentFragment();

$docFragment.appendXML("<basketball><teams><team>Boston Celtics</team><team>New York Knicks</team><team>Memphis Grizzlies</team></teams></basketball>");

After pulling the fragment into your main DOMDocument object, getting to where you can apply XPath expressions will also take a little more work:

 

 


$xpath = new DOMXPath($doc);

$teamNodes = $xpath.query(‘//team’);

Foreach ( $teamNodes as $teamNode ) …

 

Scala’s XML literals opens up many possibilities for concise and uncluttered code.  You can place literals directly in a pattern matching construct without having to do any special conversions:


node match {

  case <item/>

}

Scala almost carries this goodwill to a fault when it allows you to place expressions directly in the XML literals without requiring anything more than curly braces:

 

 


<item envflag={if (env == ‘production’) "prod" else "test"} >

 

Not to spoil the party, but in considering Scala one thing you will have to deal with is its strictness around variable types.  Admittedly, Scala is much more famous for its type safety than its XML literals.   Scala looks a lot like a scripting language at times, but can often surprise you with seemingly esoteric complaints about data types.  In most cases Scala can infer a type if you do not specify the  type, but you will end up with a specific variable type as opposed to a type that can keep adjusting, like Silly Putty, depending on how it is used.  Scala requires you to be a little more deliberate in how you approach an application so you don’t end up with bugs that are hard to find – or even worse, bugs you don’t find.  

Scala is just one of many languages that Artemis Consulting has developed with over the past year. Please check out our other technology blogs at: http://artemisconsultinginc.com/blog