简体   繁体   中英

JAXB - marshal with xsi:type

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;

    ...

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM