Handling change in newlines by XML transformation for CDATA from Java 8 to Java 11 Handling change in newlines by XML transformation for CDATA from Java 8 to Java 11 xml xml

Handling change in newlines by XML transformation for CDATA from Java 8 to Java 11


As your code relies on unspecified behavior, extra explicit code seems better:

  • You want indentation like:

    tform.setOutputProperty(OutputKeys.INDENT, "yes");tform.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
  • However not for elements containing a CDATA.

    String xml = result.getWriter().toString();// No indentation (whitespace) for elements with a CDATA section.xml = xml.replaceAll(">\\s*(<\\!\\[CDATA\\[.*?]]>)\\s*</", ">$1</");

The regex uses:

  • (?s) DOT_ALL to have . match any character, also newline characters.
  • .*? the shortest matching sequence, to not match "...]]>...]]>".

Alternatively: In a DOM tree (preserving CDATA) you can retrieve all CDATA sections per XPath, and remove whitespace siblings using the parent element.