如何将JAXB类实例编组为其超类 [英] How to marshal a JAXB class instance as its superclass

查看:154
本文介绍了如何将JAXB类实例编组为其超类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将JAXB带注释的类实例编组为其超类(它也是一个带注释的JAXB类)?

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;
    ...
}

扩展类型:

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

所以如果这种情况:

BenamningTYPE b = new MoreDetailedBenamningTYPE();
...

然后我想整理实例 b 作为 BenmaningTYPE 获取

<BenmaningTYPE>
    ...
</BenmaningTYPE>

NOT

<MoreDetailedBenamningTYPE>
    ...
</MoreDetailedBenamningTYPE>

如果可能的话,这个编组调用怎么样?

How would this marshal invoke look like, if possible?

推荐答案

我没有检查过,但我先试试:

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

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

如果您正在从XML Schema生成类,请同时检查可复制插件。您可以将 MoreDetailedBenamningTYPE 实例中的数据复制到实例 BenmaningTYPE 并编组。

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.

这篇关于如何将JAXB类实例编组为其超类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆