Tuesday, May 3, 2011

How to map map xs:time to java.util.Calendar?

In order to force XJC to generate java.util.Calendar we have some options,one of them is to write customization and import it in our schema or just paste <xs:annotaion> into the schema.
"<xs:schema elementformdefault="qualified" version="1.0" xs="http://www.w3.org/2001/XMLSchema" jaxb="http://java.sun.com/xml/ns/jaxb" targetnamespace="calendar-schemalet">
<xs:annotation><xs:appinfo>
<jaxb:globalbindings>
<jaxb:javatype name="java.util.Calendar" xmltype="xs:date" parsemethod="javax.xml.bind.DatatypeConverter.parseDate" printmethod="javax.xml.bind.DatatypeConverter.printDate">
<jaxb:javatype name="java.util.Calendar" xmltype="xs:time" parsemethod="javax.xml.bind.DatatypeConverter.parseDate" printmethod="javax.xml.bind.DatatypeConverter.printDate">
</jaxb:javatype></jaxb:javatype></jaxb:globalbindings>
</xs:appinfo></xs:annotation>
</xs:schema>"
Now we can import this file , for example :
<xs:import schemalocation="CalendarSchemalet.xsd" namespace="none"></xs:import>
Or we can use xjc compile parameter as following:

<!--?xml version="1.0" encoding="UTF-8"?-->
<jxb:bindings version="2.1" jxb="http://java.sun.com/xml/ns/jaxb" xsd="http://www.w3.org/2001/XMLSchema" xjc="http://java.sun.com/xml/ns/jaxb/xjc">

<jxb:globalbindings>
<xjc:javatype name="java.util.Calendar" xmltype="xsd:time" adapter="com.dpi.ach.cor.model.TimeAdapter">
</xjc:javatype></jxb:globalbindings>
</jxb:bindings>
and save this as binding.xjb, and use the following command:
xjc -p com.mycompany -b binding.xjb -extension test.xsd
that's it.