JaxB XMLGregorianCalendar Binding

JaxB does not throw an exception when binding the wrong date format to XMLGregorianCalendar. Instead, it is null. What could be the problem?

@XmlAttribute(name = "travelEndDate", required = true) @XmlSchemaType(name = "date") protected XMLGregorianCalendar travelEndDate; <xs:complexType name="SearchCriteria"> <xs:attribute name="travelStartDate" type="xs:date" use="required"/> <xs:attribute name="travelEndDate" type="xs:date" use="required"/> </xs:complexType> 
+4
source share
3 answers

Note. I am the lead EclipseLink JAXB (MOXy) and member of the JAXB 2. X ( JSR-222 ) expert group.

This behavior will change slightly between JAXB implementations. MOXy, for example, throws the following exception if the value is incorrect:

 Exception in thread "main" javax.xml.bind.UnmarshalException - with linked exception: [Exception [EclipseLink-3003] (Eclipse Persistence Services - 2.4.0.qualifier): org.eclipse.persistence.exceptions.ConversionException Exception Description: Incorrect date format: [2011-02-50] (expected [YYYY-MM-DD])] at org.eclipse.persistence.jaxb.JAXBUnmarshaller.unmarshal(JAXBUnmarshaller.java:225) at forum254.Demo.main(Demo.java:18) Caused by: Exception [EclipseLink-3003] (Eclipse Persistence Services - 2.4.0.qualifier): org.eclipse.persistence.exceptions.ConversionException Exception Description: Incorrect date format: [2011-02-50] (expected [YYYY-MM-DD]) at org.eclipse.persistence.exceptions.ConversionException.incorrectDateFormat(ConversionException.java:103) at org.eclipse.persistence.internal.oxm.XMLConversionManager.convertStringToXMLGregorianCalendar(XMLConversionManager.java:689) at org.eclipse.persistence.internal.oxm.XMLConversionManager.convertObjectToXMLGregorianCalendar(XMLConversionManager.java:278) at org.eclipse.persistence.internal.oxm.XMLConversionManager.convertObject(XMLConversionManager.java:249) at org.eclipse.persistence.oxm.XMLField.convertValueBasedOnSchemaType(XMLField.java:712) 

Bypass

To get an error message in any JAXB implementation, you can install the XML schema on unmarshaller to check:

Or you can implement an XmlAdapter to control the transformation (and, if necessary, throw an exception):

+4
source

Perhaps this link is useful to you:

http://eskatos.wordpress.com/2007/11/24/jaxb-custom-binding-for-joda-time/

It describes date processing in XML with custom Java data types (here: JodaTime)

+1
source

Use this, it will catch the exception:

 try { String date = "your date here" XMLGregorianCalendar xmlGCal = DatatypeFactory.newInstance().newXMLGregorianCalendar(date); } catch (DatatypeConfigurationException ex) { //DO STUFF } 

Update:

After adding the code, I'm not sure how you will understand this. To do this, I think you will need to perform a zero check and do what you want.

0
source

All Articles