Reputation: 2635
Using Eclipselink/MOXy 2.3 i have following usecase in marshalling to XML:
abstract class MyAbstract {
}
class MyImpl extends MyAbstract {
}
class A {
private MyAbstract myAbstract;
// MyImpl is behind this
public MyAbstract getMyAbstract() {
return myAbstract;
}
}
I have following mapping defined in oxm.xml:
<java-type name="foo.MyAbstract" xml-accessor-type="NONE">
<xml-see-also>
foo.MyImpl
</xml-see-also>
</java-type>
<java-type name="foo.MyImpl">
<xml-root-element name="MyImpl" />
</java-type>
<java-type name="bar.A" xml-accessor-type="NONE">
<xml-root-element name="A" />
<java-attributes>
<xml-element java-attribute="myAbstract" type="foo.MyAbstract" />
</java-attributes>
</java-type>
Now this results in:
<A>
<myAbstract xsi:type="myImpl">
<!-- Mapped members of MyImpl + MyAbstract -->
</myAbstract>
</A>
Since i didnt want the property-name in the exported xml I changed:
<java-type name="bar.A" xml-accessor-type="NONE">
<xml-root-element name="A" />
<java-attributes>
<xml-element java-attribute="myAbstract" type="foo.MyAbstract" xml-path="."/>
</java-attributes>
</java-type>
which resulted in:
<A>
<!-- Members of MyImpl + MyAbstract marshalled without any wrapping element-->
</A>
What i want is:
<A>
<MyImpl>
<!-- Members of MyImpl + MyAbstract -->
</MyImpl>
</A>
The question is: how do i achieve this? MOXy is just ignoring my XmlRootElement on MyImpl...
EDIT:
Trying what Blaise suggested gives me following exception:
Exception [EclipseLink-60] (Eclipse Persistence Services - 2.3.2.v20111125-r10461):
org.eclipse.persistence.exceptions.DescriptorException
The method [] or [getMyAbstract] is not defined in the object [bar.A].
Now this needs further information which i had left out before because i thought it was not relevant:
Class A is an interface which defines: public X getMyAbstract();
MyAbstract implements X (this is why i added the type-attribute in the mapping for interface A).
So, using xml-element-ref
MOXy does not "see" the getter anymore, using xml-element
it does.
Upvotes: 2
Views: 1147
Reputation: 148977
The mapping that you are looking for is @XmlElementRef
. This corresponds to the concept of substitution groups in XML schema.
bar/oxm.xml
Below is the external mapping document for the bar
package. Note how the myAbstract property is mapped with xml-element-ref
which is the XML representation of @XmlElementRef
<?xml version="1.0" encoding="UTF-8"?>
<xml-bindings
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
package-name="bar">
<java-types>
<java-type name="A" xml-accessor-type="NONE">
<xml-root-element name="A" />
<java-attributes>
<xml-element-ref java-attribute="myAbstract"/>
</java-attributes>
</java-type>
</java-types>
</xml-bindings>
foo/oxm.xml
Below is the external metadata file for the foo
package:
<?xml version="1.0" encoding="UTF-8"?>
<xml-bindings
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
package-name="foo">
<java-types>
<java-type name="MyAbstract" xml-accessor-type="NONE">
<xml-see-also>
foo.MyImpl
</xml-see-also>
</java-type>
<java-type name="MyImpl">
<xml-root-element name="MyImpl" />
</java-type>
</java-types>
</xml-bindings>
Demo
Below is demo code for this example:
package forum8853855;
import java.util.*;
import javax.xml.bind.*;
import org.eclipse.persistence.jaxb.JAXBContextFactory;
import bar.A;
import foo.MyImpl;
public class Demo {
public static void main(String[] args) throws Exception {
List<String> oxm = new ArrayList<String>(2);
oxm.add("foo/oxm.xml");
oxm.add("bar/oxm.xml");
Map<String, Object> properties = new HashMap<String, Object>(1);
properties.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, oxm);
JAXBContext jc = JAXBContext.newInstance(new Class[] {A.class}, properties);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
A a = new A();
a.setMyAbstract(new MyImpl());
marshaller.marshal(a, System.out);
}
}
Output
<?xml version="1.0" encoding="UTF-8"?>
<A>
<MyImpl/>
</A>
For More Information
Upvotes: 2