JAXB - 列出<serializable>?</serializable>

时间:2012-01-31 14:48:41

标签: java xml xsd jaxb

我使用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。 为什么它不是对象列表?我如何添加内部元素?

2 个答案:

答案 0 :(得分:5)

请查看herehere(一定要确定您的方案)。

来自第二个链接:

<!-- 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(){...}
}

答案 1 :(得分:1)

你认为你应该在那里添加什么?我使用了类似的代,并且有这样的字段,并且期望它是字符串内容。

显示xsd可能有助于显示它来自。