JDK-8223291 : Whitespace is added to CDATA tags when using OutputKeys.INDENT to format XML
  • Type: Bug
  • Component: xml
  • Sub-Component: javax.xml.transform
  • Affected Version: 9,11,12,13
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2019-04-29
  • Updated: 2023-11-24
  • Resolved: 2019-07-03
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 14
14 b05Fixed
Related Reports
Relates :  
Description
ADDITIONAL SYSTEM INFORMATION :
Confirmed in both 11.0.3 and 12.0.1

A DESCRIPTION OF THE PROBLEM :
When formatting an XML file using javax.xml.transform.Transformer with OutputKeys.INDENT set to yes, additional whitespace is added before and after CDATA tags.

REGRESSION : Last worked in version 8u212

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Format the XML below using javax.xml.transform.Transformer with OutputKeys.INDENT set to yes

<foo><bar><![CDATA[data]]></bar></foo>

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
<foo>
  <bar><![CDATA[data]]></bar>
</foo>
ACTUAL -
<foo>
  <bar>
    <![CDATA[data]]>
  </bar>
</foo>

---------- BEGIN SOURCE ----------
    @Test
    public void test() throws TransformerException, ParserConfigurationException, IOException, SAXException
    {
        String data = "data";

        StreamSource source = new StreamSource(new StringReader("<foo><bar><![CDATA[" + data + "]]></bar></foo>"));
        StreamResult result = new StreamResult(new StringWriter());

        Transformer tform = TransformerFactory.newInstance().newTransformer();
        tform.setOutputProperty(OutputKeys.INDENT, "yes");
        tform.transform(source, result);

        String xml = result.getWriter().toString(); // This String contains the extra whitespace

        Document document = DocumentBuilderFactory.newInstance()
            .newDocumentBuilder()
            .parse(new InputSource(new StringReader(xml)));

        String resultData = document.getElementsByTagName("bar")
            .item(0)
            .getTextContent();

        assertEquals(data, resultData);

---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
None currently known

FREQUENCY : always



Comments
Goetz made a comment in the PR whether the two predecessors could be skipped (https://github.com/openjdk/jdk11u-dev/pull/1532#issuecomment-1342986221). I'm removing the labels on this bug and the other two for the time being, until this enquiry is answered.
20-12-2022

[jdk11u fix request] I am requesting this change be backported in order to fix the bug in jdk11u. This change was reviewed by phohensee as it was not marked clean (due to differences in Last Updated dates, and a list of bugids). The change fixes the issue in the bug described above, and adds additional testing: risk is low.
29-11-2022

Like you said, It's a bug fix. So no way (feature/property) to get back to the erroneous behavior.
28-11-2022

Is there a way of reenabling this behavior(yes, bug) with a configuration to the XML transformer?
25-11-2022

A pull request was submitted for review. URL: https://git.openjdk.org/jdk11u-dev/pull/1532 Date: 2022-11-18 23:40:29 +0000
18-11-2022

A pull request was submitted for review. URL: https://git.openjdk.org/jdk11u-dev/pull/1521 Date: 2022-11-14 22:21:13 +0000
14-11-2022

URL: http://hg.openjdk.java.net/jdk/jdk/rev/25165403c62e User: joehw Date: 2019-07-03 16:30:50 +0000
03-07-2019

The issue started with JDK 9 b150 . To reproduce the issue , run the test case with the assertions enabled. JDK 8u212 - Pass JDK 9 b149 - Pass JDK 9 b150 - Fail JDK 9.0.4 - Fail JDK 11- Fail JDK 11.0.3 - Fail JDK 12.0.1 - Fail JDK 13-ea+17 - Fail Exception in thread "main" java.lang.AssertionError at JI9060450.main(JI9060450.java:35)
03-05-2019