繁体   English   中英

JAXB Bindings文件:验证错误

[英]JAXB Bindings file: validation error

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

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>

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

所以官方版本强制你使用其他(比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 namespace)包含Sun对JAXB的扩展。

显然,这个bug仍然没有修复。 使用<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>

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

解决方法是,只需添加<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编辑器。

我遇到了同样的错误,我通过<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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM