我对使用jaxb进行编组很新,我正试图从我的对象中创建这个xml:
<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 ...
我正在使用以下代码创建这些对象:
JAXBElement element = new JAXBElement(
new QName("http://www.centralbrokersystem.org", "Proces_Bericht_Result"), TypeProcesBerichtResultV2.class, typeProcesBerichtResultV2);
我必须创建一个JAXBElement,因为TypeProcesBerichtResultV2类没有使用@RootElement注释,并且它是使用jaxB maven插件生成的,因此我无法更改它 .
然后我正在调用一种方法:
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?
我正在尝试编组的元素是以下生成的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;
...
1 回答
我通过更改以下语句修复了它:
变成:
和
变成
请注意我现在如何使用baseClass作为类型而不是实际的类进行编组 . 这会广告xsi:type标记 .