Java: Writing a DOM to an XML file (formatting issues)
I'm assuming that you're using a Transformer
to do the actual writing (to a StreamResult
). In which case, do this before you call transform
:
transformer.setOutputProperty(OutputKeys.INDENT, "yes");transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
There are a few good examples of "pretty printing" in the following thread