Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's a non-annotation Joda solution. We have generated objects from xsd's and want them to use Joda instead of XmlGregorianCalendar.</p> <p>Note: When I tried to pass a proper XmlGregorianCalendar object to the unmarshal methods in the classes, I got JaxB compiler errors that said it required type String, not XmlGregorianCalendar. Tested with String, and it appears to be working fine. Quick and dirty error handling here, so fix that up as you please.</p> <p>Hope this helps.</p> <p>Maven pom plugin snippet:</p> <pre><code> &lt;plugin&gt; &lt;groupId&gt;org.jvnet.jaxb2.maven2&lt;/groupId&gt; &lt;artifactId&gt;maven-jaxb2-plugin&lt;/artifactId&gt; &lt;configuration&gt; &lt;schemaDirectory&gt;src/main/resources/schemas/&lt;/schemaDirectory&gt; &lt;removeOldOutput&gt;true&lt;/removeOldOutput&gt; &lt;bindingIncludes&gt; &lt;bindingInclude&gt;jaxb-custom-bindings.xml&lt;/bindingInclude&gt; &lt;/bindingIncludes&gt; &lt;/configuration&gt; &lt;executions&gt; &lt;execution&gt; &lt;goals&gt; &lt;goal&gt;generate&lt;/goal&gt; &lt;/goals&gt; &lt;/execution&gt; &lt;/executions&gt; &lt;/plugin&gt; </code></pre> <p>jaxb-custom-bindings.xml file:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;bindings xmlns="http://java.sun.com/xml/ns/jaxb" version="2.0" xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt; &lt;globalBindings&gt; &lt;javaType name="org.joda.time.DateTime" xmlType="xs:dateTime" parseMethod="com.yourcompanyname.XSDDateTimeToJodaDateTimeMarshaller.unmarshal" printMethod="com.yourcompanyname.XSDDateTimeToJodaDateTimeMarshaller.marshal" /&gt; &lt;javaType name="org.joda.time.LocalDate" xmlType="xs:date" parseMethod="com.yourcompanyname.XSDDateToJodaLocalDateMarshaller.unmarshal" printMethod="com.yourcompanyname.XSDDateToJodaLocalDateMarshaller.marshal" /&gt; &lt;/globalBindings&gt; </code></pre> <p></p> <pre><code>public class XSDDateTimeToJodaDateTimeMarshaller { private static final Logger LOG = LoggerFactory.getLogger(XSDDateTimeToJodaDateTimeMarshaller.class); public static DateTime unmarshal(String xmlGregorianCalendar) { DateTime result= new DateTime(xmlGregorianCalendar); return result; } public static String marshal(DateTime dateTime) { String result = "MARSHALLING_ERROR"; try { result = DatatypeFactory.newInstance().newXMLGregorianCalendar(dateTime.toGregorianCalendar()).toXMLFormat(); } catch (DatatypeConfigurationException e) { LOG.error("Error marshalling Joda DateTime to xmlGregorianCalendar",e); } return result; } </code></pre> <p>}</p> <pre><code> public class XSDDateToJodaLocalDateMarshaller { private static final Logger LOG = LoggerFactory.getLogger(XSDDateToJodaLocalDateMarshaller.class); public static LocalDate unmarshal(String xmlGregorianCalendar) { return new LocalDate(xmlGregorianCalendar); } public static String marshal(LocalDate localDate) { String result = "MARSHALLING_ERROR"; try { result = DatatypeFactory.newInstance().newXMLGregorianCalendar(localDate.toDateTimeAtStartOfDay().toGregorianCalendar()).toXMLFormat(); } catch (DatatypeConfigurationException e) { LOG.error("Error marshalling Joda LocalDate to xmlGregorianCalendar",e); } return result; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload