The presentation "Best Practices for XML/Schema Data Binding with JAXB" (25364) was held in the Hilton San Francisco's Imperial Ballroom A in the late afternoon of the second day (Tuesday) of JavaOne 2011 and I was starting to feel some mental fatigue. I wanted to attend this session because I have a love/hate relationship with JAXB. On the one hand, I love the power of JAXB. On the other hand, I loathe some of the things one cannot easily do in terms of customization of the Java classes generated from XML schema.
Martin Grebac (Oracle Czech Republic) presented to a relatively well-attended (almost standing-room only) audience, but it was difficult to hear at first with the main hall behind the room's open door (fortunately someone closed the door a few minutes in). It also didn't help that people kept coming in late, opening and closing the door, and the presentation next door ("Meet the Java Posse") had a rowdy bunch. Grebac started with laying down groundwork such as defining XML data binding and differentiating between direct and indirect XML data binding. He also talked about XML data binding being based on various grammars such as XSD and DTD.
Grebac looked at some XML databinding limitations. He talked about "feature limitations" (incomplete schema support, transformations, and document fragment support) and "round-trip limitations" (including sibling order, physical structure, comments and processing instructions, and XML declaration).
One of Grebac's slides stated, "Issues start from schema." A follow-up side showed the other direction and discussed "Issues start from classes." Grebac said that the issues and limitations can be depressing. He showed a slide "W3C - The Ultimate Response" that contained some ideas to help. These included some databinding patterns. Grebac showed some example slides of these patterns and stated that
unqualified should generally be used for
attributeFormDefault and that
qualified should generally not be used. He added that
xs:documentation should be used instead of
The "Common-sense Alternative" Grebac proposed avoids the "long list of patterns to keep in mind" and instead focuses on keeping it short and simple (his version of "KISS"). As part of this, Grebac made the following recommendations (with justification for each recommendation):
- "Avoid problematic primitive types."
- "Be Explicit: Use @XmlSchemaType"
- Use XmlGregorianCalendar in Java classes rather than Date and Calendar.
- Avoid anonymous types [Don't use
- Avoid mixed content (@XmlMixed) of any type
- Don't rely on type inheritance by extension
- Don't rely on type inheritance by restriction
- Keep it Simple
I am hoping that Grebac makes his slides available because he has a late slide full of links to additional references and I'd like to be able to look at some of his examples with the smiley faces for good practices and red cross-out symbols for bad practices.