1
votes

I have a Parent and a Child class.

A House class has a field of type Parent which can refer to a Child object. I need to map it to XML using Eclipse Moxy.

Its xsd would be something like:

<xs:complexType name="Parent" abstract="true"> ...other fields...

<xs:complexType name="Child" > <xs:extension base="Parent"> ...other fields...

<xs:element name="child" type="Child" substitutionGroup="parent" /> <xs:element name="parent" type="Parent" abstract="true" />

<xs:complexType name="House"> <xs:element ref="parent"/>

House class contains a JAXBElement to point to Parent: @XmlElementRef(name = "parent", namespace = "abc", type = JAXBElement.class) protected JAXBElement<? extends Parent> parent;

How do I map House class through House.oxm.xml file so this polymorphic mapping works correctly?

<java-type name="House" xml-accessor-type="NONE"> <java-attributes> <xml-element-ref java-attribute="?????????"/>

I tried using '@' in the mapping but it doesn't work - it just adds the reference String (@Parent) of the object to XML.

1

1 Answers

1
votes

The whole issue was because of:

@XmlElementRef(name = "parent", namespace = "abc", type = JAXBElement.class) protected JAXBElement<? extends Parent> parent;

After many futile attempts to fix it, I came across this defect: https://bugs.eclipse.org/bugs/show_bug.cgi?id=327811

After referring to its code, I saw that it addressed a situation very similar to mine, but used just @XmlElementRef annotation on the supertype, without using anything else like JAXBElement or supplying any other parameters to @XmlElementRef.

I tried that (and removed its mapping from oxm file) and it worked like a charm! I hope this answer helps anybody else who is stuck with the same problem.