Take the 2-minute tour ×

I am getting object of type org.w3c.dom.Document from a source (basically a parsed xml document - java object). I want to use this xml document object to create java objects using JAXB. How can I do that?

share|improve this question
Please accept previous answers!!! –  Cratylus Feb 27 '12 at 10:48

2 Answers 2

up vote 2 down vote accepted

You can unmarshal directly from DOM objects, this includes a org.w3c.dom.Document:

If you need to specify the type of object you are unmarshalling, then you can wrap the DOM node in a javax.xml.transform.dom.DOMSource and use the following API:

share|improve this answer

From the top of my head you could just convert the Document to a Stream and use the Unmarshaller to convert to the JAXB instance you need.

Something like:

MyJAXB o = (MyJAXB)unmarshaller.unmarshall(new StringReader(arrayoutsream.tostring()));  

Disclaimer: Haven't even bothered to see if this compiles but you should understand how to approach this

share|improve this answer
FYI - JAXB (JSR-222) has APIs for unmarshalling directly from DOM nodes: stackoverflow.com/a/9464111/383861 –  Blaise Doughan Feb 27 '12 at 11:29

Your Answer


