users@jaxb.java.net

Re: _at_XmlJavaTypeAdapter questions

From: Wolfgang Laun <wolfgang.laun_at_gmail.com>
Date: Tue, 26 Aug 2008 08:38:36 +0200

On Mon, Aug 25, 2008 at 4:13 PM, Melchior, Joris <Joris.Melchior_at_fmr.com> wrote:
> Hi Everyone,
>
> I'm working from an existing schema that is not really ideal for creating
> our models but I have little control over it so I'm trying to convert things
> using binding instructions in an external (xjb) file.
>
> One of things I'm struggling with is that I can't find a way to change the
> 'depth' of the model, for example, we get things with this structure;
>
> <monthyeartype>
> <month>01</month>
> <year>2005</year>
> </monthyeartype>
>
> For our purposes we would like to convert this to a java.util.Date or
> joda.LocalDate.

Ideally, the XML schema designer should have used
<xsd:element name="monthyeartype" type="xsd:gYearMonth"/>
and that would have been that. (JAXB would have given you an
XMLGregorianCalendar object.)

> Are the javaType conversions only meant for simple xml types or can complex
> types be converted as well? If so, where can I find some
> documentation/samples of this?

The customizing language doesn't have a way for defining type mappings for
complex types. If you annotate (hand-written) Java classes you could provide
a type adapter that's capable of anything fancy. (See the recent thread
"XmlJavaTypeAdapter help" on this list.)

If you can't have your schema changed, you might map <month> and <year>
from (presumably) xsd:int to xsd:gMonth and xsd:gYear, which would give you
two XMLGregorianCalendar objects. Post-processing on <monthyeartype>
elements could transfer the month value into the year object.

[More generally speaking, I should say that an XML structure is defined
for serializing/transmitting data, and that's not necessarily what you want or
need while your application is churning through that data. - We have written
an XML schema and all participating applications, and we still have a
transformation between the content tree and the objects used by the
application code.]

Cheers
Wolfgang