从XSD文件生成类

时间:2018-11-21 18:44:56

标签: xml xsd jaxb xjc

我正在尝试通过使用JAXB(仅出于培训目的)从spring-beans.xsd生成Java类,并且出现了以下错误:

parsing a schema...
[ERROR] Property "Ref" is already defined. Use <jaxb:property> to resolve this conflict.
  line 582 of file:/C:/spring-beans.xsd

[ERROR] The following location is relevant to the above error
  line 622 of file:/C:/spring-beans.xsd

[ERROR] Property "Value" is already defined. Use <jaxb:property> to resolve this conflict.
  line 584 of file:/C:/spring-beans.xsd

[ERROR] The following location is relevant to the above error
  line 629 of file:/C:/spring-beans.xsd

[ERROR] Property "Key" is already defined. Use <jaxb:property> to resolve this conflict.
  line 1135 of file:/C:/spring-beans.xsd

[ERROR] The following location is relevant to the above error
  line 1138 of file:/C:/spring-beans.xsd

[ERROR] Property "Ref" is already defined. Use <jaxb:property> to resolve this conflict.
  line 1052 of file:/C:/spring-beans.xsd

[ERROR] The following location is relevant to the above error
  line 1071 of file:/C:/spring-beans.xsd

[ERROR] Property "Value" is already defined. Use <jaxb:property> to resolve this conflict.
  line 1054 of file:/C:/spring-beans.xsd

[ERROR] The following location is relevant to the above error
  line 1078 of file:/C:/spring-beans.xsd

Failed to parse a schema.

怎么了?感谢您的任何提前帮助。

1 个答案:

答案 0 :(得分:0)

问题在于,每种情况下您都有两个XML项-像这样的元素

<xsd:element ref="ref"/>

和类似

的属性
<xsd:attribute name="ref" type="xsd:string"/>

在相同的复杂类型中。它们默认都映射到名为Ref的属性。但是由于这两个XML项是不同的东西,JAXB抱怨命名冲突。

通常,解决方案非常简单(错误日志实际上建议了该解决方案)。 使用jaxb:property绑定来自定义属性之一。大致情况:

<jaxb:bindings version="1.0" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <jaxb:bindings 
        schemaLocation="spring-beans.xsd" 
        node="/xs:schema">
        <jaxb:bindings node="xs:element[@name='constructor-arg']/xs:complexType/xs:attribute[@name='ref']">
            <jaxb:property name="aRef"/>
        </jaxb:bindings>
    </jaxb:bindings>

</jaxb:bindings>