简体   繁体   English

如何编组JAXBElement <Object> 使用JAXB进入Java中的org.w3c.dom.Element

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

相关问题 如何将对象(JAXB)或byte [](从此对象)转换为org.w3c.dom.Element? - How can I convert object (JAXB) or byte[] (from this object) to `org.w3c.dom.Element`? 您可以将 jaxb object 转换为 org.w3c.dom.Element 吗? - can you convert a jaxb object to org.w3c.dom.Element? 如何设置“ org.w3c.dom.element”的第一个孩子? - How to set the first child of `org.w3c.dom.element`? 将带有 org.w3c.dom.Element 的 java 对象转换为字符串并在转换回来时出错 - Convert java object with org.w3c.dom.Element to string and get error when convert back 通过org.w3c.dom.Element对象查找作为org.dom4j.Document上的参数(将org.w3c.dom.Element转换为org.dom4j.Element) - find by org.w3c.dom.Element object as parameter on org.dom4j.Document (convert org.w3c.dom.Element to org.dom4j.Element) 如何在java中将org.w3c.dom.Element输出为字符串格式? - How to I output org.w3c.dom.Element to string format in java? 从org.w3c.dom.Element强制转换为SVGSVGElement - cast to SVGSVGElement from org.w3c.dom.Element fails org.w3c.dom.Element setAttribute(“ xlink:href”,文件名) - org.w3c.dom.Element setAttribute(“xlink:href”, filename) org.w3c.dom.Element在父级中获得位置 - org.w3c.dom.Element get position in parent 如何处理Javafx org.w3c.dom.Element强制转换为MenuItem错误? - How to handle Javafx org.w3c.dom.Element cast to MenuItem error?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM