This gives me a Document object with a top level node with no child nodes:
public static Document getDocument(Object jaxb)
{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
//dbf.setNamespaceAware(true);
Document doc = dbf.newDocumentBuilder().newDocument();
JAXBContext context = JAXBContext.newInstance(jaxb.getClass());
context.createMarshaller().marshal(jaxb, doc);
return doc;
}
This is the workaround, which seems even more inefficient, since it converts to String and then to Document.
public static Document getDocument(Object jaxb)
{
StringWriter writer = new StringWriter();
JAXBContext context = JAXBContext.newInstance(jaxb.getClass());
context.createMarshaller().marshal(jaxb, writer);
return DocumentBuilderFactory.newInstance().newDocumentBuilder().
parse(new InputSource(new StringReader(writer.toString()));
}
Is it possible to accomplish what I'm trying to accomplish?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…