in log4j-changelog/src/main/java/org/apache/logging/log4j/changelog/util/XmlWriter.java [109:131]
private static String serializeXmlDocument(final Document document, final String rootElementName) throws Exception {
final Transformer transformer = TransformerFactory.newInstance().newTransformer();
final StreamResult result = new StreamResult(new StringWriter());
final DOMSource source = new DOMSource(document);
transformer.setOutputProperty(OutputKeys.ENCODING, CHARSET_NAME);
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
transformer.transform(source, result);
// Life is too short to solve DOM transformer issues decently
final String xml = result.getWriter().toString();
final String padding = StringUtils.repeat(" ", rootElementName.length() + 2);
return xml.replace("?><!--", "?>" + LS + "<!--")
.replace("--><", "-->" + LS + "<")
.replaceFirst(
'<' + rootElementName + " (.+>" + LS + ")",
('<' + rootElementName + " xmlns=\"" + XmlUtils.XML_NAMESPACE + "\"" + LS + padding
+ "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" + LS + padding
+ "xsi:schemaLocation=\"" + XmlUtils.XML_NAMESPACE + " " + XmlUtils.XML_SCHEMA_LOCATION
+ "\"" + LS + padding
+ "$1"));
}