[英]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.