Pretty-printing output from javax.xml.transform.Transformer with only standard java api (Indentation and Doctype positioning)

The missing part is the amount to indent. You can set the indentation and indent amount as follow:

transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
transformer.transform(xmlInput, xmlOutput);

Leave a Comment