如何使用XStream for List <object>?</object>

时间:2014-10-07 10:47:44

标签: java jaxb xstream

我想根据JAXB和XStream进行映射。

以下是代码片段:

@XmlElements({
        @XmlElement(name = "Success", type = SuccessType.class),
        @XmlElement(name = "Warnings", type = WarningsType.class),
        @XmlElement(name = "BagTypes", type = HashMap.class)
})
protected List<Object> successAndWarningsAndBagTypes;

如何使用XStream为List<Object>制作类似的注释映射?

或者更简单,更好的是将此List<Object>划分为单独的类实例?

更新

我必须根据这个xml文件映射这段代码:

<EI_BaggageTypesRS Version="1.0" xmlns="http://www.opentravel.org/OTA/2003/05">
    <Success/>
    <BagTypes>
        <ResponseBagType>
            <code>AA</code>
            <description>Golf Bag</description>
        </ResponseBagType>
        <ResponseBagType>
            <code>BA</code>
            <description>Skis</description>
        </ResponseBagType>
        <ResponseBagType>
            <code>DA</code>
            <description>Snow Board</description>
        </ResponseBagType>
        <ResponseBagType>
            <code>CA</code>
            <description>Fishing Gear</description>
        </ResponseBagType>
        <ResponseBagType>
            <code>EA</code>
            <description>Surf Board</description>
        </ResponseBagType>
    </BagTypes>
</EI_BaggageTypesRS>

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

假设代码是从XML模式生成的,我将回答这个问题:

  

或者更简单,更好的是将此列表分开   类实例?

请参阅this answer。分割successAndWarningsAndBagTypes的一种方法是使用我的Simplify plugin。您可以使用simplify:as-element-property将一个@XmlElements属性拆分为多个@XmlElement(最后没有s)属性。例如,从中:

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

你会得到这个:

@XmlElement(name = "a", type = String.class)
protected List<String> a;
@XmlElement(name = "b", type = Integer.class)
protected List<Integer> b;

但是我不确定如何使用wsimport的JAXB插件。一定是可能的,只是从未尝试过。

相关问题