JAXB binding file: XmlAdapters and package name

Puce picture Puce · Mar 17, 2011 · Viewed 22.2k times · Source

I have a binding file like this

<jxb:bindings version="2.0" xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <jxb:bindings schemaLocation="example.xsd" node="/xs:schema">
    <jxb:schemaBindings>
        <jxb:package name="example" />
    </jxb:schemaBindings>
    <jxb:globalBindings>
        <jxb:javaType name="java.util.Calendar" xmlType="xs:dateTime"
            parseMethod="javax.xml.bind.DatatypeConverter.parseDateTime"
            printMethod="javax.xml.bind.DatatypeConverter.printDateTime" />
        <jxb:javaType name="java.util.Calendar" xmlType="xs:date"
            parseMethod="javax.xml.bind.DatatypeConverter.parseDate"
            printMethod="javax.xml.bind.DatatypeConverter.printDate" />
        <jxb:javaType name="java.util.Calendar" xmlType="xs:time"
            parseMethod="javax.xml.bind.DatatypeConverter.parseTime"
            printMethod="javax.xml.bind.DatatypeConverter.printTime" />
    </jxb:globalBindings>

  </jxb:bindings>
</jxb:bindings>

The schema class are generated in "example" (correct), but the XmlAdapters in "org.w3._2001.xmlschema" (wrong). How can I fix this?

Answer

skaffman picture skaffman · Mar 17, 2011

The org.w3._2001.xmlschema package is created here because XJC has to generate a class that extends javax.xml.bind.annotation.adapters.XmlAdapter, which in turn calls your parse/print static methods. For some reason, it puts them into this package, rather than somewhere more useful.

You haven't said which JAXB implementation you use, but the JAXB RI has an extension to the javaType binding customization which allows you to specify a subclass of XmlAdapter directly, rather than parseMethod/printMethod pairs. This removes the need to generate the synthetic XmlAdapter bridge class. See the RI docs for how to do this.

I imagine EclipseLink/Moxy has something similar to this, but I'm not sure if the XJC that ships with Java6 is capable of it (Sun seems to have removed half of the useful stuff from the RI when they brought it into the JRE).