As I mentioned in "Generating XML Schemas from ISO 20022 UML models", I am using Scala to write an implementation of the ISO 20022 method for generating XML Schemas from a UML model. The code is in two parts; the first part processes a MagicDraw UML model via the MagicDraw Open API (a Java API) and produces a simple XML representation of the ISO 20022 message information that is contained in the UML model. That simple XML representation can then be processed to produce resources such as the XML Schemas; in a later iteration, I also plan to support generation of ASN.1 specifications for the same messages. ASN.1 is the way that ISO 20022 messages will be able to be binary encoded rather than XML encoded, for situations where volumes require something more compact than textual XML. At the moment, I have just finished writing the code for generating the XML Schemas from the simple XML representation (having previously written the code to generate the simple XML representation from a MagicDraw UML model).
Back to Scala and XML, a major reason for using Scala rather than Java for this code is that Scala has better built-in support for XML. In spite of some really top XML people having worked at Sun in the last decade, the XML facilities in Java have never been more than an awkward tacked-on piece that continues to make Java developers resistant to using XML. Scala goes a long way to addressing that problem, and making XML straightforward to work with.
For example, in Scala I can write
val x = <document><h1>Example of inline XML in Scala code</h1></document>
which creates a value "x" of type "scala.xml.Node". Note that I didn't have to put the XML in quotes to convert it into a string. Scala knows what XML is, so you can use XML directly.
If you need to insert values into the XML, you can do it like this:
val famousText = "To be or not to be"
val speaker = "Hamlet"
val famousPhrase = <phrase speaker={speaker}>{famousText}</phrase>
and the value of "famousPhrase" is
<phrase speaker="Hamlet">To be or not to be</phrase>
Braces are used to indicate where something in the XML should be intepreted as code. For attributes, if the attribute value is contained in braces, the enclosing quotes are omitted. Some of you may think that this looks not unlike ASP or JSP, and in some ways it does, but it is neatly integrated into Scala. These expressions can appear in any Scala code, they aren't forced into separate files as you typically have with ASP and JSP. Also, the resultant expressions are understood by the code to be XML, not just text with angle brackets. I can extract values from the XML using a path notation:
val extractedSpeakerAttr = famousPhrase \ "@speaker"
val extractedSpeaker = extractedSpeakerAttr.text
The result of this is that "extractedSpeakerAttr" is an XML node sequence with one node, an attribute; "extractedSpeaker" is a string with the value "Hamlet".
This is why I like Scala for working with XML. It's a full general purpose programming language, like Java/C#/etc., but the code you write for working with XML is short and to the point, comparable to XQuery. For my XML Schema generator, it seems like the best solution. Brevity is still one of the best ways to keep your code maintainable, and to keep the cost of that maintenance down to a minimum.
By the way, if you want to try out these Scala example, just download and install Scala, and type "scala" at the command line. That starts the Scala interpreter; you can type the above code, and it will print out the results. It's an easy way to get a feel for Scala and what it can do.