Women in Technology

Hear us Roar

  Understanding JAXB: Java Binding Customization
Subject:   Date problem...
Date:   2003-12-26 12:41:15
From:   anonymous2
My xsd defines a bunch of elements that hold dates. When I unmarshall an xml file, my dates are not correct. Any ideas???
Full Threads Newest First

Showing messages 1 through 6 of 6.

  • Date problem...
    2003-12-29 17:00:28  hashimisayed [View]

    JAXB maps an xs:date to a java.util.Calendar. Because dates can be represented in various formats, you might have to customize how the date is parsed and what type of object to return after the parsing. Since your dates are not correct, then more than likely the format of your dates does not match what java.util.Calendar is expecting. So, what you want to do is write a little class that parses your dates for you. To do that, you have to write some JAXB customization and a simple class.

    Lets assume that you want all the dates in your schema to be treated the same and that when JAXB sees an xs:date it should return a java.util.Date and not the default java.util.Calendar. For simplicity, lets also assume that your dates are formatted as: "yyyy-mm-dd".

    1) Add JAXB Customization to the globalBindings section:

    <jaxb:javaType name="java.util.Date" xmlType="xs:date" printMethod="SimpleDateFormatter.print" parseMethod="SimpleDateFormatter.parse"/>

    2) Write a class that formats the date and returns a java.util.Date.

    public class SimpleDateFormatter
    private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    public static Date parse(String date)throws ParseException
    return dateFormat.parse(date);
    public static String print(Date date)
    String result = null;
    result = dateFormat.format(date);
    return result;

    A couple of things to realize.

    1) If you have to write a class to format and return dates, then you will have to make that class available to the schema compiler when you compile the xsd; you do that by executing the jar file (jaxb-xjc.jar) and using the classpath (-cp) switch.
    2) You maybe able to use an existing class. For example, if my dates were really formatted as "yyyy-MM-dd", then you could use java.sql.Date and not have to write a new class and have to deal with (1).
    3) Finally, if all of your dates are not formatted the same then you need to write xml simpleTypes for the dates and add customization to each one separately. Here is an example:

    <xs:simpleType name="Date">
    <jaxb:javaType name="java.sql.Date" xmlType="xs:date" printMethod="valueOf" parseMethod="toString"/>
    <xs:restriction base="xs:date"/>


    • Date problem...
      2005-03-22 02:38:57  matmayer [View]

      Correction to date customization using java.sql.date :

      <jxb:javaType name="java.sql.Date" xmlType="xs:date" printMethod="toString" parseMethod="valueOf"/>

      It will work better ;-)
    • Date problem...
      2005-03-23 11:26:53  Jaxnitup [View]

      I hav a similiar issue....

      I am getting the following eror when trying to compile the Java object using the xjc script

      Here is the portion of my XSD which uses the custom JXB declarations:
      <xs:complexType name="ccbndatetype" mixed="true">
      <jxb:javaType name="java.util.Date" parseMethod="com.calpine.webservice.client.xml.ccbn.CCBNDateConverter.parseStringToDate" printMethod="com.calpine.webservice.client.xml.ccbn.CCBNDateConverter.parseDateToString"/>
      <xs:extension base="xs:string">
      <xs:attribute name="Date" type="xs:int" use="required"/>
      <xs:attribute name="Time" type="xs:time" use="required"/>

      When i run the following...
      %JAXB_HOME%\bin\xjc.bat -extension ccbn-complex-global.xsd -p com.calpine.webservice.client.xml.ccbn

      It returns this error:
      parsing a schema...
      [ERROR] <javaType> customization in this context must be nested (JAXB spec sec 6.8.1):
      <javaType ...>
      line 17 of ccbn-complex-global.xsd

      Line 17 is the '<xs:appinfo>' just above the custom jxb:JavaType tag

      PLEASE help!!!!
      • Date problem...
        2005-11-22 02:47:57  daolwin [View]

        If you never fixed it, just surround your <jxb:javaType ...> with <property>

        I had the same problem and it worked that way.

  • Date problem...
    2004-12-09 03:32:27  AnupK [View]

    i tried to write on customized jaxb :
    following is the content of my.xsd
    <jxb:javaType name="java.util.Date" xmlType="xsd:date" printMethod="SimpleDateFormatter.print" parseMethod="SimpleDateFormatter.parse"/>
    then i compile it using xjc commond.


    i am getting the followng error...
    parsing a schema...
    compiling a schema...
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:324)
    at org.apache.commons.launcher.ChildMain.run(ChildMain.java:269)
    Caused by: java.lang.NoClassDefFoundError: SimpleDateFormatter
    at com.sun.tools.xjc.grammar.xducer.UserTransducer.generateDeserializer(UserTransducer.java:165)
    at com.sun.tools.xjc.grammar.xducer.TransducerDecorator.generateDeserializer(TransducerDecorator.java:56)
    at com.sun.tools.xjc.grammar.xducer.WhitespaceTransducer.generateDeserializer(WhitespaceTransducer.java:67)
    at com.sun.tools.xjc.generator.unmarshaller.PerClassGenerator.generateEatTextFunction(PerClassGenerator.java:422)
    at com.sun.tools.xjc.generator.unmarshaller.PerClassGenerator.eatText(PerClassGenerator.java:384)
    at com.sun.tools.xjc.generator.unmarshaller.TextMethodGenerator.performTransition(TextMethodGenerator.java:143)
    at com.sun.tools.xjc.generator.unmarshaller.HandlerMethodGenerator.onState(HandlerMethodGenerator.java:184)
    at com.sun.tools.xjc.generator.unmarshaller.HandlerMethodGenerator.generate(HandlerMethodGenerator.java:249)
    at com.sun.tools.xjc.generator.unmarshaller.PerClassGenerator.generate(PerClassGenerator.java:192)
    at com.sun.tools.xjc.generator.unmarshaller.UnmarshallerGenerator._generate(UnmarshallerGenerator.java:91)
    at com.sun.tools.xjc.generator.unmarshaller.UnmarshallerGenerator.generate(UnmarshallerGenerator.java:55)
    at com.sun.tools.xjc.Driver.generateCode(Driver.java:384)
    at com.sun.tools.xjc.Driver.run(Driver.java:220)
    at com.sun.tools.xjc.Driver._main(Driver.java:80)
    at com.sun.tools.xjc.Driver.access$000(Driver.java:46)
    at com.sun.tools.xjc.Driver$1.run(Driver.java:60)

    Can u plz help me....
    anup K
    • Date problem...
      2004-12-09 05:55:55  hashimisayed [View]

      It looks like the compiler cannot find your custom class. You need to make the class available to the xjc compiler. Compile you schema with the schema compiler (jaxb-xjc.jar) and use the -cp switch to tell the compiler where your class is.