Behavior

XMLField differs from most xml-to-object mapping frameworks because it does not alter or delete data which is not part of its model, even with write operations (ie. adding tags, setting values, ordering items).

Suppose an application has been written to work on the following document as input using a xml-to-object framework. This application adds and updates a specific tag : "lastLogin"

<user>
    <id>sdupond</id>
    <lastName>John</lastName>
    <firstName>Doe</firstName>
</user>

At some point, we decided to add some tags to the model, without updating the application.

<user source=‘leads’>
    <id>sdupond</id>
    <lastName>John</lastName>
    <firstName>Doe</firstName>

    <preferences>
          <rememberMe>true</rememberMe>
    </preferences>
</user>

Lets see the differences between XmlField and most of the other frameworks :

Generic xml-to-object (not XmlField)

With most frameworks, xml is read and mapped to an object. The application updates this object then creates a new xml file from it. All data from the initial xml document which did not fit into the object are simply lost. As a result, you get the following file :

<user>
    <id>sdupond</id>
    <lastName>John</lastName>
    <firstName>Doe</firstName>
    <lastLogin>2011/10/10</lastLogin>
</user>

XmlField

XmlField does not type to map every xml attribute to an object. Instead, it uses an annotated interface to create a view on the xml file. This means that when calling write methods, you are simply updating the underlying xml document. Even when reordering arrays, XmlField keeps track of each object and moves the corresponding tag accordingly. As a result, you get the following file :

<user source=‘leads’>
    <id>sdupond</id>
    <lastName>John</lastName>
    <firstName>Doe</firstName>

    <preferences>
          <rememberMe>true</rememberMe>
    </preferences>
    
     <lastLogin>2011/10/10</lastLogin>
</user>

Why does it matter

When building an enterprise-grade application, divided into multiple components, applications and servers, updating object or document models can be very time consuming because you need to support the new model in every application.

With XmlField (and REST services working on xml documents), you can add data to your model without updating the whole system in most cases. The only applications which need to be updated are the ones REALLY using the new data.

XmlField comes with a validation module (also working only on known data) which can help you to implement this without loosing document validation.

comments powered by Disqus