XML Document to String XML Document to String xml xml

XML Document to String


Assuming doc is your instance of org.w3c.dom.Document:

TransformerFactory tf = TransformerFactory.newInstance();Transformer transformer = tf.newTransformer();transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");StringWriter writer = new StringWriter();transformer.transform(new DOMSource(doc), new StreamResult(writer));String output = writer.getBuffer().toString().replaceAll("\n|\r", "");


First you need to get rid of all newline characters in all your text nodes. Then you can use an identity transform to output your DOM tree. Look at the javadoc for TransformerFactory#newTransformer().