< xs:choice>的XJC Java类生成不受限制的元素 [英] XJC Java class generation for <xs:choice> element which is not unbounded

查看:311
本文介绍了< xs:choice>的XJC Java类生成不受限制的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在昨天这个类似的问题之后,我还有另一个关于XML架构和XJC绑定中继承的问题。

After this similar question yesterday I have another question concerning inheritance in XML schema and XJC bindings.

给定以下选项元素,使Book和Journal具有共同的父类型( Publication )。

Given the following choice element such that Book and Journal have a common parent type (Publication).

<xsd:choice >
    <xsd:element name="Book" type="Book" />
    <xsd:element name="Journal" type="Journal" />
</xsd:choice>

生成的Java类属性如下:

The Java class properties which are generated are like:

private Book book;
private Journal journal;

由于< xsd:choice> 表示可能有预订期刊我希望

Since <xsd:choice> means that there might be either a Book or a Journal I would prefer

private Publication bookOrJournal;

如果通过设置我有一个出版物列表maxOccurs =unbounded在choice元素中,它会以这种方式工作,我会得到

If I had a list of Publications by setting maxOccurs="unbounded" in the choice element, it would work that way and I would get

private List<Publication> bookOrJournal;

如何使用非集合属性实现此目的?

How to achieve this with a non-collection property?

推荐答案

您可以使用以下 XJC绑定实现此目的。

You can using the following XJC binding to achieve this.

<xs:complexType name="myClass">
  <xs:sequence>
    <xs:choice>
      <xs:annotation>
        <xs:appinfo>
          <jaxb:property name="bookOrJournal"/>
        </xs:appinfo>
      </xs:annotation>
      <xs:element name="Book" type="Book"/>
      <xs:element name="Journal" type="Journal"/>
    </xs:choice>
  </xs:sequence>
</xs:complexType>

执行 xjc< XSD文件> -extension ,这为我生成了以下Java类。

After executing xjc <XSD File> -extension, this generated the following Java class for me.

@XmlElements({
    @XmlElement(name = "Book", type = Book.class),
    @XmlElement(name = "Journal", type = Journal.class)
})
protected Publication bookOrJournal;

要使用XJC绑定,我在XSD的顶部添加了以下内容。

To use the XJC binding, I added the following to the top of my XSD.

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

这篇关于&lt; xs:choice&gt;的XJC Java类生成不受限制的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆