我正在尝试将对象设为org.w3c.dom.Element,但它不起作用。 这是我的代码:
public static org.w3c.dom.Element writeXMLElement(Object nf) {
JAXBContext context;
try {
context = JAXBContext.newInstance(Rodo.class);
Marshaller marshaller = context.createMarshaller();
org.w3c.dom.Element retorno = new org.w3c.dom.Element() {
...
};
marshaller.marshal(nf, retorno);
return retorno;
} catch (JAXBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
如果我写XML文件,它可以正常工作。但是对于Element我得到了这个错误:
线程“main”java.lang.NullPointerException中的异常 at com.sun.xml.internal.bind.marshaller.SAX2DOMEx.startElement(Unknown Source) at com.sun.xml.internal.bind.v2.runtime.output.SAXOutput.endStartTag(Unknown Source) at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.endAttributes(Unknown Source) at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsSoleContent(Unknown Source) at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.serializeRoot(Unknown Source) at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsRoot(Unknown Source) at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(Unknown Source) at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.marshal(Unknown Source) 在javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(未知来源) 在XMLWriter.writeXMLElement(XMLWriter.java:388) 在HTMLCTeParse.getCTeBean(HTMLCTeParse.java:339) 在GeraCTe.main(GeraCTe.java:12)
有谁知道如何解决它? 谢谢! BR
答案 0 :(得分:0)
我找到了其他解决方案:
How to marshall JAXBElement<Object> into org.w3c.dom.Element in Java using JAXB
你可以编组到DOMResult:
DOMResult res = new DOMResult();的Marshaller.marshal(myJaxbElement, RES);元素elt =((Document)res.getNode())。getDocumentElement();
无论如何,谢谢你。 :)