11

以下 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 绑定文件?

4

5 回答 5

12

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"on<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 的扩展。

于 2012-10-16T12:28:55.957 回答
2

作为解决方法,只需添加<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 编辑器中对我有用。

于 2016-04-14T09:16:37.307 回答
1

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

<jaxb:globalBindings>
  <xjc:javaType
    name="org.joda.time.LocalDate"
    xmlType="xs:date"
    adapter="org.example.XmlDateAdapter" />
</jaxb:globalBindings>
于 2015-10-12T09:24:01.697 回答
1

我遇到了同样的错误,我通过将元素的前缀从(xmlns:jaxb="http://java.sun.com/xml/ns/jaxb") 更改为<javaType>jaxbxjc(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>
于 2018-01-14T13:04:03.430 回答
0

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

于 2016-01-06T15:01:33.737 回答