繁体   English   中英

如何编组JAXBElement <Object> 使用JAXB进入Java中的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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM