我使用 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 不可序列化。
为什么它不是对象列表?如何添加内部元素?
请看一下here https://stackoverflow.com/questions/10940267/jaxb-xmlmixed-usage-for-reading-xmlvalue-and-xmlelement and here http://docs.oracle.com/javaee/5/api/javax/xml/bind/annotation/XmlMixed.html(肯定应该解决您的情况)。
来自第二个链接:
<!-- 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(){...}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)