Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to marshall JAXBElement<Object> into org.w3c.dom.Element in Java using JAXB

Tags:

java

element

jaxb

I need to get Element object with request. I have ObjectFactory. I created an JAXBElement, and I need to marshall it to Element. Could anyone help me?

like image 224
Constantine Gladky Avatar asked Nov 26 '12 17:11

Constantine Gladky


2 Answers

You could marshal to a DOMResult:

DOMResult res = new DOMResult();
marshaller.marshal(myJaxbElement, res);
Element elt = ((Document)res.getNode()).getDocumentElement();
like image 92
Ian Roberts Avatar answered Jan 04 '23 20:01

Ian Roberts


In addition to Ian's answer, I suppose to first create a Document, as the unchecked cast can be omitted this way:

Document document =
    DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
JAXB.marshal(jaxbElement, new DOMResult(document));
Element element = document.getDocumentElement();
like image 34
poseidon Avatar answered Jan 04 '23 20:01

poseidon