本文介绍了JAXB - 列表< Serializable>?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用xjc创建了一些类。

I made some classes using xjc.

    public class MyType {

    @XmlElementRefs({
        @XmlElementRef(name = "MyInnerType", type = JAXBElement.class, required = false),

    })
    @XmlMixed
    protected List<Serializable> content;

    public List<Serializable> getContent() {
        if (content == null) {
            content = new ArrayList<Serializable>();
        }
        return this.content;
    }
}

但我无法使用

getContent().add(newItem);

因为MyInnerType不是Serializable。
为什么它不是对象列表?如何添加内部元素?

because MyInnerType is not Serializable.Why its not a List of Objects? How do i add inner elements?

推荐答案

请看一下和(一个应该确定你的方案)。

Please take a look here and here (one should for sure address your scenario).

来自第二个链接:

<!-- schema fragment having  mixed content -->
<xs:complexType name="letterBody" mixed="true">
<xs:sequence>
    <xs:element name="name" type="xs:string"/>
    <xs:element name="quantity" type="xs:positiveInteger"/>
    <xs:element name="productName" type="xs:string"/>
    <!-- etc. -->
</xs:sequence>
</xs:complexType>
<xs:element name="letterBody" type="letterBody"/>


// Schema-derived Java code: 
// (Only annotations relevant to mixed content are shown below, 
//  others are ommitted.)
import java.math.BigInteger;
public class ObjectFactory {
    // element instance factories
    JAXBElement<LetterBody> createLetterBody(LetterBody value);
    JAXBElement<String>     createLetterBodyName(String value);
    JAXBElement<BigInteger> createLetterBodyQuantity(BigInteger value);
    JAXBElement<String>     createLetterBodyProductName(String value);
  // type instance factory
    LetterBody> createLetterBody();
}

public class LetterBody {
    // Mixed content can contain instances of Element classes
    // Name, Quantity and ProductName. Text data is represented as
    // java.util.String for text.
    @XmlMixed 
    @XmlElementRefs({
            @XmlElementRef(name="productName", type=JAXBElement.class),
            @XmlElementRef(name="quantity", type=JAXBElement.class),
            @XmlElementRef(name="name", type=JAXBElement.class)})
    List getContent(){...}
}

这篇关于JAXB - 列表&lt; Serializable&gt;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 19:53