[英]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.