dom4j icon indicating copy to clipboard operation
dom4j copied to clipboard

XMLWriter removes standalone attribute from XML header

Open PAX523 opened this issue 5 years ago • 1 comments

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));
  }

PAX523 avatar May 09 '19 15:05 PAX523

I'd be glad when the formatter preserves all possible attributes in XML header that are present in the input XML.

PAX523 avatar May 10 '19 06:05 PAX523