JAXB-使用xsi:type进行封送 [英] JAXB - marshal with xsi:type
问题描述
我对使用jaxb进行编组有点陌生,我正在尝试从我的对象中创建此xml:
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>
我得到的是以下内容:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Proces_Bericht_result xmlns="http://www.centralbrokersystem.org">
<Result_Data>
...
</Result_Data>
</Proces_Bericht_result>
我想定义xsi:type ...
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);
我必须创建一个JAXBElement,因为TypeProcesBerichtResultV2类没有用@RootElement注释,并且它是用jaxB maven插件生成的,所以我不能更改它.
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)
,该方法的实现是:
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;
}
我应该更改为编组为正确的xml吗?
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);
更改为:
JAXBElement element = new JAXBElement(
new QName("http://www.centralbrokersystem.org", "Proces_Bericht_Result"), TypeProcesBerichtResultBase.class, typeProcesBerichtResultV2);
和
XmlUtils.object2Xml(element, TypeProcesBerichtResultV2.class)
更改为
XmlUtils.object2Xml(element, TypeProcesBerichtResultBase.class)
请注意,我现在如何将baseClass作为类型而不是实际的类进行编组.这会投放xsi:type标记.
Notice how I'm now marshalling with the baseClass as type instead of the actual class. This ads the xsi:type tag.
这篇关于JAXB-使用xsi:type进行封送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!