java - JAXB @XmlAdapter for arbitrary XML -
i have org.w3c.dom.element i'm returning xmladapter custom @xmlelement , i'd include part of jaxb object arbitrary xml (i'm aware i'll have hand-craft xsd). however, jaxb complains with
org.w3c.dom.element interface, , jaxb can't handle interfaces. apparently w3c xml types not supported java types, shame. further this, same error when use javax.xml.transform.result apparently supported.
how can include arbitrary xml elements elements in jaxb?
note: per https://forums.oracle.com/thread/1668210 i've tried
messagefactory factory = messagefactory.newinstance(); message = factory.createmessage(); soapelement element = message.getsoapbody().adddocument(doc); but giving same error.
tl;dr
you can have xmladapter converts domain object instance of org.w3c.dom.element long specify value type object (not element).
below full example.
xmladapter
a field/property of type java.lang.object keep unknown content dom nodes. can leverage in use case specifying value type in xmladapter object. need ensure root element returned marshal method matches field/property defined @xmlelement annotation.
import javax.xml.bind.annotation.adapters.xmladapter; import javax.xml.parsers.*; import org.w3c.dom.*; public class baradapter extends xmladapter<object, bar>{ private documentbuilder documentbuilder; public baradapter() { try { documentbuilderfactory dbf = documentbuilderfactory.newinstance(); documentbuilder = dbf.newdocumentbuilder(); } catch(exception e) { // todo - handle exception } } @override public bar unmarshal(object v) throws exception { bar bar = new bar(); element element = (element) v; bar.value = element.gettextcontent(); return bar; } @override public object marshal(bar v) throws exception { document document = documentbuilder.newdocument(); element root = document.createelement("bar"); root.settextcontent(v.value); return root; } } java model
foo
the @xmljavatypeadapter annotation used reference xmladapter.
import javax.xml.bind.annotation.*; import javax.xml.bind.annotation.adapters.xmljavatypeadapter; @xmlrootelement @xmlaccessortype(xmlaccesstype.field) public class foo { @xmljavatypeadapter(baradapter.class) private bar bar; } bar
import javax.xml.bind.annotation.*; @xmlaccessortype(xmlaccesstype.field) public class bar { string value; } demo code
demo
since there cost creating documentbuilderfactory can leverage jaxb's ability handle stateful instances of xmladapter setting instance on marshaller.
import java.io.file; import javax.xml.bind.*; public class demo { public static void main(string[] args) throws exception { jaxbcontext jc = jaxbcontext.newinstance(foo.class); unmarshaller unmarshaller = jc.createunmarshaller(); file xml = new file("src/forum18272059/input.xml"); foo foo = (foo) unmarshaller.unmarshal(xml); marshaller marshaller = jc.createmarshaller(); marshaller.setadapter(new baradapter()); marshaller.setproperty(marshaller.jaxb_formatted_output, true); marshaller.marshal(foo, system.out); } } input.xml/output
<?xml version="1.0" encoding="utf-8"?> <foo> <bar>hello world</bar> </foo>
Comments
Post a Comment