Puce Puce - 4 years ago 387
Java Question

JAXB Bindings file: validation error

The following JAXB bindings file creates the Adapter classes as expected, but Eclipse and XMLSpy say it's no valid:

<?xml version="1.0" encoding="UTF-8"?>
<jxb:bindings xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd" version="2.1">


<jxb:globalBindings>
<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:dateTime" parseMethod="javax.xml.bind.DatatypeConverter.parseDateTime"
printMethod="javax.xml.bind.DatatypeConverter.printDateTime" />
<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>


The error is something like:

cvc-complex-type.2.4.b: The content of element 'jxb:globalBindings' is not complete. One of '{"http://java.sun.com/xml/ns/jaxb":javaType, "http://java.sun.com/xml/ns/jaxb":serializable, WC[##other:"http://java.sun.com/xml/ns/jaxb"]}' is expected.


Note that the JAXB bindings schema file references top-level elements using the prefix "jaxb".

How can I create a valid JAXB bindings file?

Answer Source

http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd is wrong, but no one can do anything about it :(

The problem is in the definition of globalBindings global element. It looks like this:

  <xs:element name="globalBindings">
    <xs:annotation>
      <xs:documentation>Customization values defined in global scope.</xs:documentation>
    </xs:annotation>
    <xs:complexType>
      <xs:sequence minOccurs="0">
        <xs:element ref="jaxb:javaType" minOccurs="0" maxOccurs="unbounded" />
        <xs:element ref="jaxb:serializable" minOccurs="0" />
        <xs:any namespace="##other" processContents="lax">
          <xs:annotation>
            <xs:documentation>allows extension binding declarations to be specified.</xs:documentation>
          </xs:annotation>
        </xs:any>
      </xs:sequence>
      ...
    </xs:complexType>

But it should look like this:

  <xs:element name="globalBindings">
    <xs:annotation>
      <xs:documentation>Customization values defined in global scope.</xs:documentation>
    </xs:annotation>
    <xs:complexType>
      <xs:sequence minOccurs="0">
        <xs:element ref="jaxb:javaType" minOccurs="0" maxOccurs="unbounded" />
        <xs:element ref="jaxb:serializable" minOccurs="0" />
        <xs:any namespace="##other" processContents="lax" minOccurs="0" maxOccurs="unbounded">
          <xs:annotation>
            <xs:documentation>allows extension binding declarations to be specified.</xs:documentation>
          </xs:annotation>
        </xs:any>
      </xs:sequence>
      ...
    </xs:complexType>

Mind the minOccurs="0" maxOccurs="unbounded" on <xs:any /> element.

So the official version forces you to use other (than JAXB's) element insinde globalBindings. You can look at [http://jaxb.java.net/nonav/2.0/binding-customization/http.java.sun.com.xml.1306680588/index.html](http://java.sun.com/xml/ns/jaxb/xjc namespace) which contains Sun's extensions to JAXB.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download