【问题标题】:JAXB Bindings file: validation errorJAXB 绑定文件:验证错误
【发布时间】:2012-10-16 12:16:25
【问题描述】:

以下 JAXB 绑定文件按预期创建适配器类,但 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 绑定文件?

【问题讨论】:

    标签: java xsd jaxb


    【解决方案1】:

    作为解决方法,只需将&lt;xsd:any/&gt; 添加为&lt;globalBindings&gt; 的最后一个孩子

    这是一个示例:

    <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 编辑器中对我有用。

    【讨论】:

      【解决方案2】:

      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>
      

      注意&lt;xs:any /&gt; 元素上的minOccurs="0" maxOccurs="unbounded"

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

      【讨论】:

      • 谢谢,我的测试表明您似乎是正确的。我在这里提出了一个问题:java.net/jira/browse/JAXB-924 随意投票和/或评论。
      • @l3dx,解决​​方法很简单,只需添加一些##Other 标签。在 OP 的情况下:&lt;xsd:annotation&gt;&lt;xsd:documentation&gt;Use built in date conversion support&lt;/xsd:documentation&gt;&lt;/xsd:annotation&gt;
      【解决方案3】:

      我遇到了同样的错误,我通过 &lt;javaType&gt; 元素的前缀从 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>
      

      【讨论】:

        【解决方案4】:

        显然,该错误仍未修复。 &lt;xsd:annotation&gt;&lt;xsd:documentation&gt;Use built in date conversion support&lt;/xsd:documentation&gt; 的提示对我不起作用,因为我收到“不支持的绑定命名空间 http://www.w3.org/2001/XMLSchema”(已翻译)错误。而是使用以下语法可以正常工作:

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

        【讨论】:

          【解决方案5】:

          对我来说,将前缀从 xs 更改为 xsd 很有效。我只能想象原因是我的 wsdl 使用 xsd 前缀定义了命名空间。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-04-18
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-12-17
            • 1970-01-01
            相关资源
            最近更新 更多