Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8223291

Whitespace is added to CDATA tags when using OutputKeys.INDENT to format XML

XMLWordPrintable

    • b150
    • 9
    • b05
    • generic
    • generic

      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


            joehw Joe Wang
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            9 Start watching this issue

              Created:
              Updated:
              Resolved: