JAXB Bindings文件:验证错误

时间:2012-10-16 12:16:25

标签: java xsd jaxb

以下JAXB绑定文件按预期创建Adapter类,但Eclipse和XMLSpy表示它无效:

<?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>

错误类似于:

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.

请注意,JAXB绑定模式文件使用前缀“jaxb”引用顶级元素。

如何创建有效的JAXB绑定文件?

5 个答案:

答案 0 :(得分:9)

http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd是错误的,但没有人可以做任何事情:(

问题在于globalBindings全局元素的定义。它看起来像这样:

  <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>

但它应该是这样的:

  <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>

注意minOccurs="0" maxOccurs="unbounded"元素上的<xs:any />

所以正式版强制你使用其他(比JAXB的)元素insinde globalBindings。您可以查看[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命名空间),其中包含Sun对JAXB的扩展。

答案 1 :(得分:1)

显然,这个bug仍然没有修复。 <xsd:annotation><xsd:documentation>Use built in date conversion support</xsd:documentation>的提示对我不起作用,因为我得到了一个不支持的绑定命名空间http://www.w3.org/2001/XMLSchema&#34; (翻译)错误。而是使用以下语法正常工作:

<jaxb:globalBindings>
  <xjc:javaType
    name="org.joda.time.LocalDate"
    xmlType="xs:date"
    adapter="org.example.XmlDateAdapter" />
</jaxb:globalBindings>

答案 2 :(得分:0)

对我而言,它可以将前缀从xs更改为xsd。我只能想象,原因是我的wsdl使用xsd-prefix定义名称空间。

答案 3 :(得分:0)

作为解决方法,只需添加<xsd:any/>作为<globalBindings>

的最后一个孩子

以下是一个示例:

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

这在验证期间适用于STS 3.7.3 xml编辑器。

答案 4 :(得分:0)

我遇到了同样的错误,我通过<javaType>更改jaxb元素的前缀来解决它(xmlns:jaxb =“http://java.sun.com/xml / ns / jaxb“)到xjc (xmlns:xjc =”http://java.sun.com/xml/ns/jaxb/xjc“)。

因此,此代码出现错误:

<?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>

固定代码如下所示:

<?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>