How to marshal a JAXB class instance as its superclass

▼魔方 西西 提交于 2019-12-01 09:04:30

问题


Is it possible to marshal a JAXB annotated class instance as its superclass (which is also a JAXB annotated class)?

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "BenamningTYPE", propOrder = {"benamningId"})
@XmlSeeAlso({MoreDetailedBenamningTYPE.class})
public class BenamningTYPE {
    ...
    @XmlElement(name = "BenamningId", required = true)
    protected IdentifierTYPE benamningId;
    ...
}

And the extended type:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "MoreDetailedBenamningTYPE", propOrder = {"modifyDetails"})
public class MoreDetailedBenamningTYPE extends BenamningTYPE {
    ...
    @XmlElement(name = "ModifyDetails", required = true)
    protected ModifyDetailsTYPE modifyDetails;
    ...
}

So if this scenario:

BenamningTYPE b = new MoreDetailedBenamningTYPE();
...

Then I would like to marshal the instance b as BenmaningTYPE to get

<BenmaningTYPE>
    ...
</BenmaningTYPE>

And NOT:

<MoreDetailedBenamningTYPE>
    ...
</MoreDetailedBenamningTYPE>

How would this marshal invoke look like, if possible?


回答1:


I haven't checked it, but I'd try first:

new JAXBElement(new QName("BenmaningType"),
    BenManingTYPE.class, moreDetailedBenmaningTYPEInstance)

If you're generating classes from an XML Schema, check also the copyable plugin. You could the copy data from an instance of MoreDetailedBenamningTYPE to the instance BenmaningTYPE and marshal that.

There are more possibilities, but they're a bit more complex.



来源:https://stackoverflow.com/questions/4264422/how-to-marshal-a-jaxb-class-instance-as-its-superclass

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!