[英]How to marshall JAXBElement<Object> into org.w3c.dom.Element in Java using JAXB
I need to get Element object with request. 我需要获取带有请求的Element对象。 I have ObjectFactory. 我有ObjectFactory。 I created an JAXBElement, and I need to marshall it to Element. 我创建了一个JAXBElement,我需要将它编组到Element。 Could anyone help me? 谁能帮助我?
You could marshal to a DOMResult
: 你可以编组到DOMResult
:
DOMResult res = new DOMResult();
marshaller.marshal(myJaxbElement, res);
Element elt = ((Document)res.getNode()).getDocumentElement();
In addition to Ian's answer, I suppose to first create a Document, as the unchecked cast can be omitted this way: 除了Ian的回答之外,我想首先创建一个Document,因为可以通过这种方式省略未经检查的强制转换:
Document document =
DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
JAXB.marshal(jaxbElement, new DOMResult(document));
Element element = document.getDocumentElement();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.