Skip to content

Instantly share code, notes, and snippets.

@mnuessler
Last active May 18, 2021 10:14
Show Gist options
  • Save mnuessler/721a54739d4e7486c808 to your computer and use it in GitHub Desktop.
Save mnuessler/721a54739d4e7486c808 to your computer and use it in GitHub Desktop.
public class XmlIndent {
/*
* Format XML with indentation
*/
private static String prettyFormat(InputStream input, int indent) {
try {
Source xmlInput = new StreamSource(input);
StringWriter stringWriter = new StringWriter();
StreamResult xmlOutput = new StreamResult(stringWriter);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", String.valueOf(indent);
transformer.transform(xmlInput, xmlOutput);
return xmlOutput.getWriter().toString();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment