TL DR
You can have an XmlAdapter that converts your domain object into an instance of org.w3c.dom.Element , while you specify the value type as Object (not Element ).
The following is a complete example.
XMLAdapter
A field / property of type java.lang.Object will store unknown content as DOM nodes. You can use this in your use case by specifying the value type in the XmlAdapter as Object . You will need to make sure that the root element returned by the marshal method matches the field / property defined by the @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) {
Java Model
Foo
The @XmlJavaTypeAdapter annotation @XmlJavaTypeAdapter used to refer to the 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 is the cost of creating a DocumentBuilderFactory, we can use the capabilities of JAXB to process instances with XmlAdapter state correction by installing the 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>
source share