Last active
May 18, 2021 10:14
-
-
Save mnuessler/721a54739d4e7486c808 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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