Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object to XML, backward and forward compatibility

I am working in an application where we need to save objects in XML format, and load them later once required. For this I have used JAXB to marshall and unmarshall XMLs back to Java classes.

My problem is that the I have to change Java models sometime (by adding, renaming or deleting attributes), as a result, I will have incompatible saved XMLs which can’t be bound back to the new class form.

To solve this, every time I have to do a change I take a copy of all the classes under a new package (named after its version) and apply the requested changes. And when saving an XML I save its version so that I can decide which package should be scanned by JAXB to unmarshall this XML.

My question is, Is there any other way to implement backward and forward compatibility using JAXB? If not is there any other technology that can support this?

like image 831
Transient Avatar asked Mar 04 '11 12:03

Transient


People also ask

What is forward compatibility and backward compatibility?

Backward compatibility is a design that is compatible with previous versions of itself. Forward compatibility is a design that is compatible with future versions of itself.

Is JSON forward and backward compatible?

Full compatibility – the previous version of the schema and the new version are both backward compatible and forward compatible.

What can be solved using backward compatibility?

Backward compatibility can be used to preserve older software that would have otherwise been lost when a manufacturer decides to stop supporting older hardware. Classic video games are a common example used when discussing the value of supporting older software.

Why backwards and forward compatibility is needed in Android development?

Forward compatibility is important because many Android-powered devices receive over-the-air (OTA) system updates. The user may install your application and use it successfully, then later receive an OTA update to a new version of the Android platform.


1 Answers

Note: I am a member of the JAXB 2 (JSR-222) expert group and lead EclipseLink JAXB (MOXy).

For this use case I prefer to use a single model when possible. This will require that you have multiple mappings for your object model. The JAXB spec does not provide a means to do this, but it can be done using MOXy's externalized metadata extension:

  • http://bdoughan.blogspot.com/2010/12/extending-jaxb-representing-annotations.html

The metadata can be used to supplement the annotations, or used to replace them. So I would recommend mapping your base schema with annotations and using the XML format to modify the metadata per version of the schema.

My problem is that the I have to change Java models sometime (by adding, renaming or deleting attributes) , as a result, I will have incompatable saved XMLs which can’t be bound back to the new class form.

Deleting a Java attribute (field/property) makes things difficult since there will be nothing for the old XML to map to. Instead you can leave them in your model, and mark them "@XmlTransient" in the XML metadata files.

like image 125
bdoughan Avatar answered Nov 13 '22 18:11

bdoughan