The State of JAXB: Availability, Suitability, Analysis, and Architecture
Subject:   Looking for POJO serialization
Date:   2004-05-10 07:40:34
From:   bazzargh
Response to: Looking for POJO serialization

JAXB isn't what you're looking. It might help the author had checked the JAXB spec to see what it was designed for:

"The [...] facility compiles an XML schema into one or more Java classes. These automatically-generated classes handle the translation between XML documents that follow the schema and interrelated instances of the derived classes. They also ensure that the constraints expressed in the schema are maintained as instances of the classes are manipulated."

ie its not a generic XML serialization tool, its been designed to ensure that you can manipulate an XML document slavishly following all the validity constraints of the schema (eg Social Security Nos have to match a regexp, not just "setSSN(String ssn) {this.ssn=ssn}"). If you're not trying to do round-trip edits on XML, or interop based on an agreed schema, JAXB is not for you.

Incidentally this is why JAXB has to use getters and setters, if you go the MS way you'll end up with data thats not been validated.

Betwixt, on the other hand, is a tool aimed at doing exactly what you are trying to do.

Personally, to produce data to match a schema "on the fly", I serialize using an in-house tool (for reasons that arent irrelevant here) to a "simple and obvious" XML format then transform the result with XSL to match the particular schema we need. Save us trying to handle fiddly details like 'elementFormDefault="qualified"' from the schema. We also transform on the way in. This method is dead easy but unlike JAXB it tends to be lossy - you just ser/deser the bits you want.

I use java's own XMLEncoder quite a bit, but it has one *big* gotcha that stops me using it more - default values of properties don't get serialized. If this was optional it'd be just dandy.