ncyoung.com

You are here: Top->Programming->Java



XML serialization of java objects

SOAP provides Java to XML conversion for a transport layer. The XML used is pretty simple and specified as part of the SOAP protocol.

Different Java serialization schemes allow you to save the state of java objects, again each in its own internal format.

Recently I ran into a requirement to convert arbitrary Java objects to java DOM objects so that they could be transformed. I was told by the developers that this would involve writing lots of one off error prone code, but I thought given the above that this problem could probably be solved in a pluggable way and in fact was very likely to have been addressed.

A little digging turned up JOX, a nifty conversion layer between beans and XML. It will try to map XML elements to bean properties, and it will try to serialize beans as XML or convert them to DOM objects (just what the doctor ordered). If you have a DTD, you can use it to inform the way that the XML or DOM gets created from the bean. Nifty.

If your bean and XML do not already have some built in correspondence, you can't use JOX.

Quick is a considerably heavier-weight solution, but it allows you a lot more flexibility in the way you map XML to java code.

Quick has its own specification for a java object to XML format binding language that lets you build custom mapping to convert arbitrary XML to arbitrary java classes with flexibility.

Update: Came upon betwixt, a bean to XML mapping mechanism from the apache project. It also uses a mapping specification to allow more flexibility than jox, but has less complexity than Quick and no code generation step. Output only goes to text string or SAX versions though, and we wanted direct bean to DOM-object conversion.