dom4j
dom4j copied to clipboard
XMLWriter removes standalone attribute from XML header
org.dom4j.io.XMLWriter.writeDeclaration()
ignores the standalone
attribute of the XML header from the input XML. The beautified XML result misses this attribute (even if it was in input XML).
static String prettyXmlString(final String rawData) throws Exception {
final Document document = DocumentHelper.parseText(rawData);
try (StringWriter result = new StringWriter()) {
final OutputFormat format = OutputFormat.createPrettyPrint();
final XMLWriter writer = new XMLWriter(result, format);
writer.write(document);
writer.close();
return result.toString();
}
}
public static void main(final String[] args) throws Exception {
String input = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?><elem></elem>";
System.out.println(prettyXmlString(input));
input = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\" ?><elem></elem>";
System.out.println(prettyXmlString(input));
}
I'd be glad when the formatter preserves all possible attributes in XML header that are present in the input XML.