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

XML declaration is not followed by a newline

XMLWordPrintable

    • b08
    • generic
    • generic
    • Not verified

        A DESCRIPTION OF THE PROBLEM :
        Using LSSerializer to pretty-print a DOM results in an xml-declaration being not followed by a newline.

        Related to bugs #7150637 and #8054115.

        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        Run the following code.

        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        <?xml version="1.0" encoding="UTF-8"?>
        <sometag/>
        ACTUAL -
        <?xml version="1.0" encoding="UTF-8"?><sometag/>

        ---------- BEGIN SOURCE ----------
        package io.github.oliviercailloux.jaris.xml;

        import java.io.StringWriter;

        import javax.xml.parsers.DocumentBuilder;
        import javax.xml.parsers.DocumentBuilderFactory;

        import org.w3c.dom.Document;
        import org.w3c.dom.Element;
        import org.w3c.dom.ls.DOMImplementationLS;
        import org.w3c.dom.ls.LSOutput;
        import org.w3c.dom.ls.LSSerializer;

        public class Snippet {
        public static void main(String[] args) throws Exception {
        final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        final DocumentBuilder builder = factory.newDocumentBuilder();
        final Document document = builder.newDocument();

        final Element child = document.createElement("sometag");
        document.appendChild(child);

        final DOMImplementationLS impl = (DOMImplementationLS) document.getImplementation().getFeature("LS", "3.0");
        final LSSerializer ser = impl.createLSSerializer();
        /** But see https://bugs.openjdk.java.net/browse/JDK-7150637 */
        /**
        * But see
        * <a href="https://bugs.openjdk.java.net/browse/JDK-8054115">8054115</a>:
        * LSSerializer remove a '\n' following the xml declaration
        */
        ser.getDomConfig().setParameter("format-pretty-print", true);
        final StringWriter writer = new StringWriter();
        final LSOutput output = impl.createLSOutput();
        output.setCharacterStream(writer);
        ser.write(document, output);

        System.out.println(writer.toString());
        }
        }

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

        FREQUENCY : always


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

                Created:
                Updated:
                Resolved: