[英]How can I convert object (JAXB) or byte[] (from this object) to `org.w3c.dom.Element`?
[英]How to marshall JAXBElement<Object> into org.w3c.dom.Element in Java using JAXB
我需要获取带有请求的Element对象。 我有ObjectFactory。 我创建了一个JAXBElement,我需要将它编组到Element。 谁能帮助我?
你可以编组到DOMResult
:
DOMResult res = new DOMResult();
marshaller.marshal(myJaxbElement, res);
Element elt = ((Document)res.getNode()).getDocumentElement();
除了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.