15

I am looking at various approaches for marshalling/unmarshalling data between Scala and XML, and I'm interested in getting community feedback (preferably grounded in first-hand knowledge/experience).

We're currently using JAXB, which is fine, but I'm hoping for a pure Scala solution. I'm considering the following approaches:

  1. Use Scala's built-in XML facilities: Scala->XML would be easy, but my guess is that the other direction would be fairly painful. On the other hand, this approach supports arbitrary translation logic.

  2. Data binding: scalaxb seems to be somewhat immature at the moment and doesn't handle our current schema, and I don't know of any other data binding library for Scala. Like JAXB, an extra translation layer is required to support involved transformations.

  3. XML pickler combinators: The GData Scala Client library provides XML pickler combinators, but recent project activity has been low and I don't know what the current status is.

Questions:

  1. What are your experiences with the approaches/libraries I've listed?
  2. What are the relative advantages and disadvantages of each?
  3. Are there any other approaches or Scala libraries that I should consider?

Edit:

I added some notes on my early impressions of pickler combinators in my own answer to this question, but I'm still very interested in feedback from someone who actually knows the various approaches in depth. What I'm hoping for is a somewhat comprehensive comparison that would help developers choose the right approach for their needs.

1
  • 1
    If you could send me the schema to (eed3si9n at gmail), I might be able to fix scalaxb. Jan 15, 2011 at 5:20

3 Answers 3

5

I recommend using Scala's built-in XML features. I've just implemented deserialization for a document structure that looks like this:

val bodyXML = <body><segment uri="foo"><segment uri="bar" /></segment></body>

Note that the segments can be nested within each other.

A segment is implemented as follows:

case class Segment(uri: String, children: Seq[Segment])

To deserialize the XML, you do this:

val mySegments = topLevelSegments(bodyXML)

...and the implementation of topLevelSegments is just a few lines of code. Note the recursion, which digs through the XML structure:

def topLevelSegments(bodyXML: Node): Seq[Segment] = 
    (bodyXML \ "segment") map { nodeToSegment }

def nodeToSegment = (n: Node) => Segment((n \ "@uri")(0) text, childrenOf(n))

def childrenOf(n: Node): Seq[Segment] = (n \ "segment") map { nodeToSegment }

Hope that helps.

2
  • I suppose this approach isn't as hairy as I expected, but I wonder how easy it is both to scale to a more complex schema and to maintain over time. A definite advantage of both data binding and pickler combinators is that you simultaneously specify the serialization/deserialization so that you don't have to worry about maintaining two parallel bodies of code. Jan 12, 2011 at 17:38
  • 2
    It's true, though, that any additional technology that you mix into your code base brings with it overhead: a syntax to learn, a set of error messages to decipher, a user group to join, possibly a deployment tweak to make. The fewer 'moving parts' the better.
    – David
    Jan 12, 2011 at 19:58
4

For comparison, I implemented David's example using the pickler combinators from the GData Scala Client library:

def segment: Pickler[Segment] =
   wrap(elem("segment", 
           attr("uri", text) 
           ~ rep(segment))) {    // rep = zero or more repetitions
      // convert (uri ~ children) to Segment(uri, children), for unpickling
      Segment.apply 
   } {
      // convert Segment to (uri ~ children), for pickling
      (s: Segment) => new ~(s.uri, s.children toList)
   }

def body = elem("body", rep(segment))

case class Segment(uri: String, children: List[Segment])

This code is all that is necessary to specify both directions of the translation between Segments and XML, whereas a similar amount of code specifies only one direction of the translation when using the Scala XML library. In my opinion, this version is also easier to understand (once you know the pickler DSL). Of course, as David pointed out in a comment, this approach requires an additional dependency and another DSL that developers have to be familiar with.

Translating XML to Segments is as simple as

body.unpickle(LinearStore.fromFile(filename)) // returns a PicklerResult[List[Segment]]

and translating the other way looks like

xml.XML.save(filename, body.pickle(segments, PlainOutputStore.empty).rootNode)

As far as the combinator library is concerned, it seems to be in decent shape and compiles in Scala 2.8.1. My initial impression is that the library is missing a few niceties (e.g. a oneOrMore combinator) that could be remedied fairly easily. I haven't had time to see how well it handles bad input, but so far it looks sufficient for my needs.

2
  • "one or more" Isn't that what rep1 does?
    – soc
    Jan 13, 2011 at 2:20
  • @soc I assume you're referring to the rep1 parser combinator in the standard library. Unfortunately, there is no such combinator in the XML pickler library. Jan 13, 2011 at 3:38
-1

Writing a scala.xml.Node to a string isn't a big deal. PrettyPrinter should take care of you needs. scala.xml.XML.save() will write to a file and scala.xml.XML.write() outputs to a Writer.

1
  • 2
    Thanks for answering, but this isn't at all what I was looking for. I'm interested in the conversion between XML documents and domain-specific object models. Jan 12, 2011 at 17:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.