这里是当前使用的代码。
public String getStringFromDoc(org.w3c.dom.Document doc) { try { DOMSource domSource = new DOMSource(doc); StringWriter writer = new StringWriter(); StreamResult result = new StreamResult(writer); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.transform(domSource,result); writer.flush(); return writer.toString(); } catch(TransformerException ex) { ex.printStackTrace(); return null; } }
依靠
DOM Level3 Load/Save:
原文链接:https://www.f2er.com/xml/293986.htmlpublic String getStringFromDoc(org.w3c.dom.Document doc) { DOMImplementationLS domImplementation = (DOMImplementationLS) doc.getImplementation(); LSSerializer lsSerializer = domImplementation.createLSSerializer(); return lsSerializer.writeToString(doc); }