Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: JAXB: Marshelling of JAXBElement<Calendar> to xs:date not correct

Tags:

java

xml

jaxb

xjc

I have a problem using JAXB. I've created my Java Classes via xjc (using an xsd-schema) and has a global binding for the XML-Datatypes xs:date xs:dateTime and xs:time to java.util.Calendar with parse and print method.

Everything works fine until I marked some xs:date fields as nillable. The xjc creates JAXBElement wrappers for those properties. If these fields get unmarshaled the date is printed out including the time information which leads to validation errors.

Is there a possibilty to force the Marshaller to convert it to xs:date instead of xs:dateTime?

Can I specify a binding for those fields which gets a special XMLAdapater ewhich converts those fields?

The property inside the Java-Class looks like that:

@XmlElementRef(name = "dateField", namespace = "namespace", type = JAXBElement.class)

protected JAXBElement<Calendar> dateField;

and the corresponding xsd-looks like

<xs:element name="dateField" minOccurs="0" nillable="true" type="xs:date" />

Can someone help me please?

Thanks and best regards,

Arne

like image 547
Arne Kröger Avatar asked Sep 27 '11 16:09

Arne Kröger


1 Answers

Do you really need to map the Calendar inside a JAXBElement?, wouldn't be correct to use the legacy XMLGregorianCalendar instead (and then you can convert it to a Date)?

like image 93
Timoteo Ponce Avatar answered Nov 06 '22 08:11

Timoteo Ponce