Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top