处理JAXB集合 [英] Dealing with JAXB Collections

查看:101
本文介绍了处理JAXB集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用JAXB解组以下XML:

I am trying to unmarshall the following XML using JAXB:

<Works>
    <Work>
        <Composers>
            <Composer>
                <Name>A name</Name>
            </Composer>
            <Composer>
                <Name>A name 2</Name>
            </Composer>
        </Composers>
    </Work>
</Works>

我使用XJC生成了所有类。如果我想访问Composers集合,我必须这样做:

I have generated all of the classes using XJC. If I want to access the Composers collection, I have to do this:

 List<Composer> composers = work.getComposers().getComposer();

我有什么方法可以做以下事情吗?

Is there any way I can do the following instead?

 List<Composer> composers = work.getComposers();

我很欣赏需要从XML派生的Composers对象,但在处理Java时,拥有存储集合的中间POJO似乎有点多余。

I appreciate the need for a Composers object as it derived from the XML, but when dealing in Java, having an intermediate POJO that stores the collections seems a bit redundant.

我的XSD是:



<xsd:complexType name="Works">
    <xsd:sequence>
        <xsd:element name="Work" type="Work" maxOccurs="unbounded" minOccurs="0"></xsd:element>
    </xsd:sequence>
</xsd:complexType>

<xsd:complexType name="Work">
    <xsd:sequence>
        <xsd:element name="Composers" type="Composers"></xsd:element>
    </xsd:sequence>
</xsd:complexType>

<xsd:complexType name="Composers">
    <xsd:sequence>
        <xsd:element name="Composer" type="Composer" maxOccurs="unbounded" minOccurs="0"></xsd:element>
    </xsd:sequence>
</xsd:complexType>

<xsd:complexType name="Composer">
    <xsd:sequence>
        <xsd:element name="Name" type="xsd:string"></xsd:element>
    </xsd:sequence>
</xsd:complexType>

推荐答案

@XmlElementWrapper 插件完全符合您的要求想要。

The @XmlElementWrapper plugin does exactly what you want.

这篇关于处理JAXB集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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