从选择中使用jaxb从xsd生成java类 [英] Generate java classes from xsd with jaxb from a choice

查看:79
本文介绍了从选择中使用jaxb从xsd生成java类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用maven和jaxb从模式中创建类。此架构有以下选择:

I use maven with jaxb to make classes out from a schema. This schema has the following choice:

<tns:choice minOccurs="1" maxOccurs="unbounded">
  <tns:element name="video_track" type="tcore:TTrack" minOccurs="1" maxOccurs="1"/>
  <tns:element name="audio_track" type="tcore:TTrack" minOccurs="1" maxOccurs="1"/>
</tns:choice>

美化是,我们想要一个 x audiotracks 和/或 x videotracks 。我们至少需要一个音频或视频轨道!

The meening is, we want a list with x audiotracks and/or x videotracks. We need at least one of an audio or a video track!

当我使用此选项生成类时,我得到以下代码:

When I generate classes with this choice I get the following code:

@XmlElementRefs({
    @XmlElementRef(name = "audio_track", type = JAXBElement.class),
    @XmlElementRef(name = "video_track", type = JAXBElement.class)
})
protected List<JAXBElement<TTrack>> videoTrackOrAudioTrack;

这不是我们想要的。我认为我的信息松散,因为 TTracks 列表我不知道这是视频还是音频。

This is not what we want. I think I loose informations because with the list of TTracks I don't know if this is a video or an audio.

那么我在这里做错了什么?

So what am I doing wrong here?

在maven中我使用的是org.jvnet.jaxb2.maven2版本0.8.3

In maven I use org.jvnet.jaxb2.maven2 version 0.8.3

推荐答案

不,你不会丢失这些信息。

No, you don't lose this information.

您将获得 JAXBElement< TTrack> 的列表。因此,您可以查看 e.getName(),了解它是音频还是视频曲目。

You get a list of JAXBElement<TTrack>. So you can check e.getName() to find out if it is an audio or a video track.

您可以使用我的简化插件来获取音频和视频曲目单独的属性。这不完全是您的架构中的模型,但非常接近并且更容易使用。

You can use my Simplify plugin to get audio and video tracks in separate properties. This is not exactly the model you have in your schema, but pretty close and much easier to use.

此自定义:

<xs:complexType name="typeWithReferencesProperty">
    <xs:choice maxOccurs="unbounded">
        <xs:element name="a" type="someType">
            <xs:annotation>
                <xs:appinfo>
                    <simplify:as-element-property/>
                </xs:appinfo>
            </xs:annotation>
        </xs:element>
        <xs:element name="b" type="someType"/>
    </xs:choice>
</xs:complexType>

产生:

@XmlElement(name = "a")
protected List<SomeType> a;
@XmlElement(name = "b")
protected List<SomeType> b;

你得到这个模型的原因是 maxOccurs 选项上

The reason you get this model is the maxOccurs on your choice.

还可以考虑使用 xs: xsd: XML Schema的前缀。

Also consider using xs: or xsd: prefixes for your XML Schema.

这篇关于从选择中使用jaxb从xsd生成java类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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