【发布时间】:2018-08-28 11:43:04
【问题描述】:
例如,我有 Java 类 (JAXB):
Test test = new Test();
test.set....
//fill test object
............
现在我需要将此对象转换为org.w3c.dom.Element
现在我有转换为byte[]的转换器:
public <T> byte[] marshal(T value) {
try {
StringWriter sw = new StringWriter();
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(value,sw);
return sw.toString().getBytes();
} catch (JAXBException e) {
throw new RuntimeException(e.getMessage());
}
}
我的问题是:如何将test 或byte[] 转换为to org.w3c.dom.Element?
编辑:
回答评论中的问题我为什么需要它
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAnyElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import org.w3c.dom.Element;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"any"
})
@XmlRootElement(name = "MessagePrimaryContent")
public class MessagePrimaryContent {
@XmlAnyElement
protected Element any;
public Element getAny() {
return any;
}
public void setAny(Element value) {
this.any = value;
}
}
我需要将我的对象设置为setAny 方法。这样的协议和格式。不是我发明的
【问题讨论】:
-
你为什么要这样做?通常,您使用 jaxb 或 dom 模型。
-
@daniu 我回答了问题
-
从
DocumentBuilderFactory.newInstance().newDocumentBuilder()创建新元素? -
@ulab 问题是关于如何将对象编组为 DOM 元素。不是关于如何简单地创建一个新的 DOM 元素。
标签: java jaxb marshalling