JAXB绑定自定义

pau*_*art 4 xml xsd jaxb

在尝试从xsd生成类时,我遇到了这个错误:

java.lang.IllegalArgumentException: Illegal class inheritance loop.  Outer class OrderPropertyList may not subclass from inner class: OrderPropertyList
Run Code Online (Sandbox Code Playgroud)

我的xsd定义了一个元素来组合一个无界元素,如下所示:

  <element minOccurs="0" name="orderPropertyList">
    <complexType>
      <sequence>
        <element maxOccurs="unbounded" name="orderProperty" type="tns:orderProperty" />
      </sequence>
    </complexType>
  </element>
Run Code Online (Sandbox Code Playgroud)

我的自定义绑定遵循此页面上的指定,但它不起作用.在这里我绑定:

<jaxb:bindings schemaLocation="../xsd/Schema.xsd" node="/xs:schema">
    <jaxb:bindings node="//xs:element[@name='orderPropertyList']">
        <jaxb:class name="OrderPropertyList"/>
    </jaxb:bindings>
</jaxb:bindings>
Run Code Online (Sandbox Code Playgroud)

我的目的是为orderPropertyList生成一个单独的类,而不是在xsd的根元素内生成内部类的默认行为.

我在这里这里看到了有同样意图的人,但它对我来说不起作用.:(

JAXB版本:

Specification-Version: 2.1
Implementation-Version: 2.1.8
Run Code Online (Sandbox Code Playgroud)

有帮助吗?

小智 13

我相信你需要做的是:

<jaxb:globalBindings localScoping="toplevel"/>
Run Code Online (Sandbox Code Playgroud)

这将生成独立类而不是嵌套类.

<jaxb:bindings schemaLocation="../xsd/Schema.xsd" node="/xs:schema">
    <jaxb:bindings node="//xs:element[@name='orderPropertyList']">
            <jaxb:class name="OrderPropertyList"/>
    </jaxb:bindings>
</jaxb:bindings>
Run Code Online (Sandbox Code Playgroud)

是一个冗余绑定,因为orderPropertyList默认会映射到OrderPropertyList.包的名称包括默认嵌套的外部类名,因此您不会更改它.

此外,如果您确实想要更改生成的类的名称,我认为XPath实际上是:

<jaxb:bindings node="//xs:element[@name='orderPropertyList']/xs:complexType">
Run Code Online (Sandbox Code Playgroud)

最后使用complexType.我认为排除这是导致您收到错误消息的原因.