开发者

JAXB marshaller to marshal super-class as root-element and use xsi:type

开发者 https://www.devze.com 2023-03-11 03:26 出处:网络
I\'m trying to marshal a sub-class as super-class with xsi:type information as the XML Root element attributes.

I'm trying to marshal a sub-class as super-class with xsi:type information as the XML Root element attributes.

Currently this is what I have (let's say..):

  • XMLDOCUMENTTYPE is a super class of XMLINVOICETYPE
  • marshalling XMLINVOICETYPE will give me <xmlInvoiceType>
  • Casting XMLINVOICETYPE instance to XMLDOCUMENTTYPE instance and then marshalling it would still give me <xmlInvoiceType>
  • Creating a wrapper object annotated with JAXB annotation seems to do the trick, but now I have to grab t开发者_运维问答he first child-node

e.g.:

jaxbWrapper.setXmlDocumentType(xmlInvoiceTypeInstance);

// will do the trick but extra root

What I'd like to get is as follow:

<XMLDOCUMENTTYPE ... ... xsi:type="XML_INVOICE_TYPE">
</XMLDOCUMENTTYPE>

Any idea how to do this?


Try marshalling:

new JAXBElement(new QName("XMLDOCUMENTTYPE"), XMLDOCUMENTTYPE.class, xmlInfoiceTypeInstance)


xsi:type is only rendered, if the type does not match the class.

Try this:

// create the type and add childs and attributes ...
XmlInVoiceType xmlInVoice = new XmlInVoiceType();
// map the element to object to force xsi:type 
final JAXBElement<?> object = new JAXBElement<>(new QName("http://your/namespace/xmlinvoice", "xmlinvoice"), Object.class, xmlInVoice);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号