Reputation: 1944
I'm kind of new to marshalling with jaxb and I'm trying to make this xml from my objects:
<Process_Bericht_Result xsi:type="Type_Proces_Bericht_Result_v2"
xmlns="http://www.centralbrokersystem.org"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
<Result_Data>
....
</Result_Data>
</Process_Bericht_Result>
What I get is the following:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Proces_Bericht_result xmlns="http://www.centralbrokersystem.org">
<Result_Data>
...
</Result_Data>
</Proces_Bericht_result>
I would like to define the xsi:type...
I'm creating these objects with the following code:
JAXBElement element = new JAXBElement(
new QName("http://www.centralbrokersystem.org", "Proces_Bericht_Result"), TypeProcesBerichtResultV2.class, typeProcesBerichtResultV2);
I have to create a JAXBElement because the TypeProcesBerichtResultV2 class isn't annotated with @RootElement and it's generated with the jaxB maven plugin so I can't change it.
Then I'm calling a method:
XmlUtils.object2Xml(element, TypeProcesBerichtResultV2.class)
and the implementation of that method is:
public static String object2Xml(Object obj,
Class clazz) {
String marshalledObject = "";
if (obj != null) {
try {
JAXBContext jc = JAXBContext.newInstance(clazz);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
new Boolean(true));
StringWriter sw = new StringWriter();
marshaller.marshal(obj, sw);
marshalledObject = new String(sw.getBuffer());
} catch (Exception ex) {
throw new RuntimeException("Unable to marshall the object", ex);
}
}
return marshalledObject;
}
What should I change to marshal to the correct xml?
The element that I'm trying to marshal is the following generated Object:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Type_Proces_Bericht_Result_v2", propOrder = {
"resultData",
"statusPartner"
})
public class TypeProcesBerichtResultV2
extends TypeProcesBerichtResultBase
{
@XmlElement(name = "Result_Data", required = true)
protected TypeResultData resultData;
...
Upvotes: 5
Views: 5750
Reputation: 412
To achive the same goal with Marshaller when creating instance of JAXBElement pass Object class in declaredType consructor parameter instead of class of marshalled object. For example:
new JAXBElement<>(new QName(namespace, obj.getClass().getSimpleName()), Object.class, null, obj);
Upvotes: 0
Reputation: 1944
I've fixed it with changing the following statements:
JAXBElement element = new JAXBElement(
new QName("http://www.centralbrokersystem.org", "Proces_Bericht_Result"), TypeProcesBerichtResultV2.class, typeProcesBerichtResultV2);
changed to:
JAXBElement element = new JAXBElement(
new QName("http://www.centralbrokersystem.org", "Proces_Bericht_Result"), TypeProcesBerichtResultBase.class, typeProcesBerichtResultV2);
and
XmlUtils.object2Xml(element, TypeProcesBerichtResultV2.class)
changed to
XmlUtils.object2Xml(element, TypeProcesBerichtResultBase.class)
Notice how I'm now marshalling with the baseClass as type instead of the actual class. This ads the xsi:type tag.
Upvotes: 4