reiley
reiley

Reputation: 3761

Change XMLGregorianCalendar to Date using external binding file (XJB)

In my maven project, I want all my datetime entries should be generated as java.util.date instead of XMLGregorianCalendar. As you might know XMLGregorianCalendar gets generated by default.

We can take example project provided here.

Here in the CustomersOrders.xsd, you can see attriute ShippedDate is of type dateTime.

<xs:attribute name='ShippedDate' type='xs:dateTime' />

To convert its data type into java.util.date, I'm following approach provided in documentation here. i.e. by using external binding file, like:

Customer.xjb

<bindings xmlns="http://java.sun.com/xml/ns/jaxb" version="2.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <globalBindings>
    <javaType name="java.util.date" xmlType="xs:datetime"
      parseMethod="javax.xml.bind.DatatypeConverter.parseDate"
      printMethod="javax.xml.bind.DatatypeConverter.printDate"
    />
  </globalBindings>
</bindings>

Then I mapped Customer.xjb file in pom.xml like:

<executions>
    <execution>
        <goals>
            <goal>generate</goal>
        </goals>
        <configuration>
            <!-- the package for the generated java classes -->
            <generatePackage>com.dimitrisli.jaxb.producedClasses</generatePackage>
            <!-- If the following not specified all xsd in resources are included -->
            <schemaIncludes>
                <include>sampleJaxb/CustomersOrders.xsd</include>
            </schemaIncludes>
            <!-- if you don't want old output -->
            <removeOldOutput>true</removeOldOutput>
            <!-- if you want verbosity -->
            <!-- verbose>true</verbose -->

            <xjbSources>
                <xjbSource>sampleJaxb/Customers.xjb</xjbSource>
            </xjbSources>

        </configuration>


    </execution>
</executions>

But when I do mvn clean install, I'm still not able to see any difference in ShippedDate, which is still being generated as XMLGregorianCalendar.

Please suggest what am I missing.

Thank You

Upvotes: 2

Views: 2785

Answers (1)

mgyongyosi
mgyongyosi

Reputation: 2667

  1. If you use the org.jvnet.jaxb2.maven2:maven-jaxb2-plugin then you should use bindingIncludes instead of xjbSources (it's for org.codehaus.mojo:jaxb2-maven-plugin).

    <bindingIncludes>
        <include>sampleJaxb/Customers.xjb</include>
    </bindingIncludes>`
    
  2. Also, you have to implement a custom adapter for java.util.Date like you have seen in the tutorial or convert to java.util.Calendar:

    <javaType name="java.util.Calendar" xmlType="xsd:dateTime"
    parseMethod="javax.xml.bind.DatatypeConverter.parseDateTime"
    printMethod="javax.xml.bind.DatatypeConverter.printDateTime" />`
    

Hope it helps!

Upvotes: 2

Related Questions