Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAXB Bindings file: validation error

Tags:

java

jaxb

xsd

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?

like image 302
Puce Avatar asked Oct 16 '12 12:10

Puce


4 Answers

As workaround, simply add <xsd:any/> as last children of <globalBindings>

Here's a sample:

<globalBindings>
    <javaType name="java.util.Calendar" xmlType="xsd:time"
        parseMethod="javax.xml.bind.DatatypeConverter.parseTime"
        printMethod="javax.xml.bind.DatatypeConverter.printTime" />
    <xsd:any/>
</globalBindings>

This works for me in STS 3.7.3 xml editor during validation.

like image 87
hejiaming007 Avatar answered Nov 19 '22 19:11

hejiaming007


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.

like image 36
Grzegorz Grzybek Avatar answered Nov 19 '22 19:11

Grzegorz Grzybek


Apparently, the bug is still not fixed. The hint with <xsd:annotation><xsd:documentation>Use built in date conversion support</xsd:documentation> didn't work for me, as I got a "not supported binding namespace http://www.w3.org/2001/XMLSchema" (translated) error. Instead using the following syntax worked fine:

<jaxb:globalBindings>
  <xjc:javaType
    name="org.joda.time.LocalDate"
    xmlType="xs:date"
    adapter="org.example.XmlDateAdapter" />
</jaxb:globalBindings>
like image 1
CodeOrion Avatar answered Nov 19 '22 20:11

CodeOrion


I got the same mistake, and I solved it by changing the prefix of <javaType> element from jaxb (xmlns:jaxb="http://java.sun.com/xml/ns/jaxb") to xjc (xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc").

So, the error was appearing for this code:

<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings xmlns="http://java.sun.com/xml/ns/jaxb"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xmlns:xs="http://www.w3.org/2001/XMLSchema"
          xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
          xmlns:jaxb="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
            http://www.w3.org/2001/XMLSchema http://www.w3.org/2001/XMLSchema.xsd"
          version="2.1">
    <jaxb:bindings schemaLocation="sci_paper_no_rdfa.xsd">
        <jaxb:globalBindings>
            <jaxb:javaType name="java.util.Date" xmlType="xs:date"
                parseMethod="rs.ac.uns.ftn.jaxb.util.MyDataTypeConverter.parseDate"
                printMethod="rs.ac.uns.ftn.jaxb.util.MyDataTypeConverter.printDate"/>
        </jaxb:globalBindings>
    </jaxb:bindings>
</jaxb:bindings>

And the fixed code looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings xmlns="http://java.sun.com/xml/ns/jaxb"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xmlns:xs="http://www.w3.org/2001/XMLSchema"
          xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
          xmlns:jaxb="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
            http://www.w3.org/2001/XMLSchema http://www.w3.org/2001/XMLSchema.xsd"
          version="2.1">
    <jaxb:bindings schemaLocation="sci_paper_no_rdfa.xsd">
        <jaxb:globalBindings> <!-- note that javaType now has xjc prefix -->
            <xjc:javaType name="java.util.Date" xmlType="xs:date"
                parseMethod="rs.ac.uns.ftn.jaxb.util.MyDataTypeConverter.parseDate"
                printMethod="rs.ac.uns.ftn.jaxb.util.MyDataTypeConverter.printDate"/>
        </jaxb:globalBindings>
    </jaxb:bindings>
</jaxb:bindings>
like image 1
Aleksandar Avatar answered Nov 19 '22 19:11

Aleksandar