xml JAXB 绑定自定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/144118/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 12:06:16  来源:igfitidea点击:

JAXB Binding Customization

xmlxsdjaxb

提问by paulosuzart

While trying to generate classes from a xsd, i got this error:

在尝试从 xsd 生成类时,我收到此错误:

java.lang.IllegalArgumentException: Illegal class inheritance loop.  Outer class OrderPropertyList may not subclass from inner class: OrderPropertyList

My xsd define a element to group a unbounded element like this:

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

  <element minOccurs="0" name="orderPropertyList">
    <complexType>
      <sequence>
        <element maxOccurs="unbounded" name="orderProperty" type="tns:orderProperty" />
      </sequence>
    </complexType>
  </element>

And my customization binding follows as specified on this page, but it doesn′t work. Here my binding:

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

<jaxb:bindings schemaLocation="../xsd/Schema.xsd" node="/xs:schema">
    <jaxb:bindings node="//xs:element[@name='orderPropertyList']">
        <jaxb:class name="OrderPropertyList"/>
    </jaxb:bindings>
</jaxb:bindings>

My intention is to generate a individual class for orderPropertyList, not the default behave that is generating a inner class inside the root element of the xsd.

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

I′ve watched someone with the same intention hereand here, but it doesn′t work properly for me. :(

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

JAXB version:

JAXB 版本:

Specification-Version: 2.1
Implementation-Version: 2.1.8

Any help?

有什么帮助吗?

回答by

I believe what you need to to is set:

我相信你需要设置的是:

<jaxb:globalBindings localScoping="toplevel"/>

This will generate standalone classes instead of nested classes.

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

Doing

正在做

<jaxb:bindings schemaLocation="../xsd/Schema.xsd" node="/xs:schema">
    <jaxb:bindings node="//xs:element[@name='orderPropertyList']">
            <jaxb:class name="OrderPropertyList"/>
    </jaxb:bindings>
</jaxb:bindings>

is a redundant binding, since orderPropertyList will map by default to OrderPropertyList. The name of the package includes the outer class name it is nested in by default, so you're not changing that.

是一个冗余绑定,因为 orderPropertyList 将默认映射到 OrderPropertyList。包的名称包括默认情况下嵌套在其中的外部类名称,因此您不会更改它。

Also, if you did want to change the name of the generated class, I think the XPath would actually be:

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

<jaxb:bindings node="//xs:element[@name='orderPropertyList']/xs:complexType">

with complexType on the end. I think excluding this was what was causing the error message you got.

以 complexType 结尾。我认为排除这一点是导致您收到错误消息的原因。

回答by Jeff Evans

It's really fun when you have a schema like the following:

当您拥有如下所示的架构时,这真的很有趣:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified" version="1.0">
    <xsd:element name="TopLevelElement">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="Something">
                    <xsd:complexType>
                        <xsd:sequence>
                            <xsd:element name="Something" maxOccurs="unbounded">
                                <xsd:complexType>
                                    <xsd:sequence>
                                        <xsd:element name="somethingFieldA" type="xsd:string"/>
                                        <xsd:element name="somethingFieldB" type="xsd:string"/>
                                    </xsd:sequence>
                                </xsd:complexType>
                            </xsd:element>
                        </xsd:sequence>
                    </xsd:complexType>
                </xsd:element>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

In this case, xjc seems to be trying to actually generate four classes called Something, one for each element named Something, and one for each of their complexTypes. So you need to provide a binding that hits each of these four elements andcomplex types specifically at the level where they occur in the schema (well really only three, because then the 4th can just become the lone Somethingclass).

在这种情况下,xjc 似乎试图实际生成四个名为 的类Something,每个名为 的元素一个,每个元素Something一个complexType。因此,您需要提供一个绑定,专门针对这四个元素复杂类型中的每一个在模式中出现的级别(实际上只有三个,因为这样第 4个元素就可以成为唯一的Something类)。

回答by dolbysurnd

I needed to do customizations for a schema like the one jeff303presented. My scenario was slightly different in that the schema was inlined within a WSDL document.

我需要对像jeff303 所示的模式进行自定义。我的场景略有不同,因为模式是内联在 WSDL 文档中的。

One thing pointed out by philvarneris that the node selection for the element should end with '/xs:complexType'and this seemed very important, as the compiler would continually generate an IllegalArgumentException related to looping inheritance without it.

philvarner指出的一件事是元素节点选择应该以 '/xs:complexType' 结尾,这似乎非常重要,因为编译器会不断生成与循环继承相关的 IllegalArgumentException 没有它。

These posts are related so I figured a link back would be helpful to someone 'googling' that ends up here.

这些帖子是相关的,所以我认为返回链接对“谷歌搜索”到这里的人会有所帮助。

Check out question 7881883

查看问题7881883

回答by amit dahiya

Entering this /xs:complexTypeat the end of the element helped in fixing the illegal class inheritance loop error.

/xs:complexType在元素的末尾输入 this有助于修复非法的类继承循环错误。

回答by David M. Karr

I believe this is happening because it's likely that the generated Java class representing the sequence of "orderProperty" is itself named "OrderPropertyList".

我相信这是因为生成的表示“orderProperty”序列的 Java 类本身很可能被命名为“OrderPropertyList”。

What I would do is first generate it without any custom bindings, and look at the class names and hierarchies that it generates. Then, consider what you want to override, and how.

我要做的是首先在没有任何自定义绑定的情况下生成它,然后查看它生成的类名和层次结构。然后,考虑您要覆盖的内容以及如何覆盖。