Monday, February 4, 2008

Multiple Representations in Object/XML Mapping

Having tried a few different approaches for Object/XML mapping over the years, from SAX/DOM manually coded work, through Rails' (ActiveRecord) toXml, JAXB, XStream, Hibernate, etc, I'm constantly surprised by the idea that most of these frameworks seem to assume that most people are going to want a single representation of an Object in XML form.

In almost every application I've ever built that did any kind of Object/XML mapping, I've wanted to have multiple representations of the objects. In particular, I'm most likely to want these three:

  • A simple reference - in this form, I'd like to link to the object, and provide almost no additional information. This is usually used to link from one object to another.
  • A summary - in this form, I'd like to provide just enough information to populate a list, or a combo-box, but no more. I'd usually use this to populate a list or a combo box.
  • The full details - in this form, I'd like to provide most or all of the information about the object. This is for detailed views of the object.
By way of example, assume that I've got a REST service for contacts in an email program (e.g. GMail). If might expose the following representations of that contact:
  • Reference
    <contact id="123" href="http://my.rest.context/api/version/contacts/123.xml" />
  • Summary
    <contact>
    <id>123</id>
    <name>Joe Blow</name>
    <email>joe.blow@example.com</email>
    </contact>
  • Full
    <contact>
    <id>123</id>
    <name>Joe Blow</name>
    <email>joe.blow@example.com</email>
    <firstName>Joe</firstName>
    <lastName>Blow</lastName>
    <address>1 Yonge Street, Toronto, ON</address>
    </contact>
In many cases, I can combine the first two; that may not be ideal in all cases, but it does help to simplify. When one object refers to another, I'd like to retain control over its representation separately. In complicated cases, I may need field-level control over the representation at various levels of an object graph. In these cases, it almost seems like I want SQL-like syntax.

I've not yet run into an Object/XML mapping framework that made this kind of control easy (do feel free to recommend some, if you like), so I've often felt like a templating approach, the same kind of approaches one uses to generate HTML, makes as much sense as any. In Rails, this takes the form of RXML templates. In Java, this could be Velocity/Freemarker, or something of that ilk.

Suggestions for another approach are welcome.

1 comment:

Unknown said...

Hi Geofrey, did you ever delve deeper into this and found a framework that can do this? I've recently been thinking about this issue also: I deal with objects that have multiple XML identities, where in one form of xml the hierarchy and element names are different from the other (i.e, symantically, the object is one complete thing, but I need to be able to represent it in two different XML formats.. Maybe more later on).