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

LSSerializer serializes DOM document with DOCTYPE, PI and root in wrong order

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Open
    • Priority: P4
    • Resolution: Unresolved
    • Affects Version/s: 8u45, 9
    • Fix Version/s: tbd
    • Component/s: xml
    • Labels:

      Description

      FULL PRODUCT VERSION :
      java version "1.8.0_45"
      Java(TM) SE Runtime Environment (build 1.8.0_45-b14)
      Java HotSpot(TM) Client VM (build 25.45-b02, mixed mode, sharing)


      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 6.3.9600]

      A DESCRIPTION OF THE PROBLEM :
      I create a W3C DOM document with

                  DOMImplementation implementation = DOMImplementationRegistry.newInstance().getDOMImplementation("XML 3.0 Core 3.0");
                  DocumentType docType = implementation.createDocumentType("FICHES", null, "docform.dtd");
                  Document doc = implementation.createDocument(null, "FICHES", docType);
                  doc.insertBefore(doc.createProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" href=\"stylesheet.xsl\""), doc.getDocumentElement());

      so the document node has three child nodes, the DOCTYPE node, the processing instruction and the root element, however when serializing with an LSSerializer the processing instruction node's serialization ends up after the root element.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Execute the code

                  DOMImplementation implementation = DOMImplementationRegistry.newInstance().getDOMImplementation("XML 3.0 Core 3.0");
                  DocumentType docType = implementation.createDocumentType("FICHES", null, "docform.dtd");
                  Document doc = implementation.createDocument(null, "FICHES", docType);
                  doc.insertBefore(doc.createProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" href=\"stylesheet.xsl\""), doc.getDocumentElement());
                  
                  DOMImplementationLS lsImp = (DOMImplementationLS)implementation;
                  LSSerializer lsSer = lsImp.createLSSerializer();
                  System.out.println(lsSer.writeToString(doc));
                  
                  for (int i = 0; i < doc.getChildNodes().getLength(); i++)
                  {
                      System.out.println(i + ": " + doc.getChildNodes().item(i).getNodeType());
                  }
                  
                  FileOutputStream outputFile1 = new FileOutputStream("test1.xml");
                  
                  LSOutput dest1 = lsImp.createLSOutput();
                  dest1.setByteStream(outputFile1);
                  
                  lsSer.write(doc, dest1);

      and check the output in the console and the created file, it shows

      <!DOCTYPE FICHES SYSTEM "docform.dtd">
      <FICHES/><?xml-stylesheet type="text/xsl" href="stylesheet.xsl"?>

      although the processing instruction is the second child node.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The serialization should have the processing instruction before the root element:

      <!DOCTYPE FICHES SYSTEM "docform.dtd">
      <?xml-stylesheet type="text/xsl" href="stylesheet.xsl"?>
      <FICHES/>
      ACTUAL -
      Wrong order of nodes in the serialization, processing instruction shows up after root element:

      <!DOCTYPE FICHES SYSTEM "docform.dtd">
      <FICHES/><?xml-stylesheet type="text/xsl" href="stylesheet.xsl"?>



      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------

      package domdoctype1;

      import org.w3c.dom.bootstrap.DOMImplementationRegistry;

      import org.w3c.dom.DOMImplementation;
      import org.w3c.dom.Document;
      import org.w3c.dom.DocumentType;

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

      import java.io.FileOutputStream;


      public class DOMDoctype1 {

          /**
           * @param args the command line arguments
           */
          public static void main(String[] args) {
              try
              {
                  DOMImplementation implementation = DOMImplementationRegistry.newInstance().getDOMImplementation("XML 3.0 Core 3.0");
                  DocumentType docType = implementation.createDocumentType("FICHES", null, "docform.dtd");
                  Document doc = implementation.createDocument(null, "FICHES", docType);
                  doc.insertBefore(doc.createProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" href=\"stylesheet.xsl\""), doc.getDocumentElement());
                  
                  DOMImplementationLS lsImp = (DOMImplementationLS)implementation;
                  LSSerializer lsSer = lsImp.createLSSerializer();
                  System.out.println(lsSer.writeToString(doc));
                  
                  for (int i = 0; i < doc.getChildNodes().getLength(); i++)
                  {
                      System.out.println(i + ": " + doc.getChildNodes().item(i).getNodeType());
                  }
                  
                  FileOutputStream outputFile1 = new FileOutputStream("test1.xml");
                  
                  LSOutput dest1 = lsImp.createLSOutput();
                  dest1.setByteStream(outputFile1);
                  
                  lsSer.write(doc, dest1);
                  
              }
              catch (Exception e)
              {
                  e.printStackTrace();
              }

          }
          
      }

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

        Attachments

          Activity

            People

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

              Dates

              Created:
              Updated: