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

DTD comments appear in DOM as a child of Document Node

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 1.2.0
    • 1.2.0
    • xml
    • 1.2.0
    • generic
    • generic
    • Verified

      19-April-2002

      The DTD comments should not appear in DOM as a child of Document Node.

      This bug can always be reproduced using xercesImpl.jar from b15-2002-04-17 build and the following program:

      ---------------------------------------------------------------
      import javax.xml.parsers.DocumentBuilderFactory;
      import javax.xml.parsers.DocumentBuilder;
      import java.io.ByteArrayInputStream;
      import org.w3c.dom.Document;
      import org.w3c.dom.Node;
      import org.apache.xml.serialize.XMLSerializer;
      import org.apache.xml.serialize.OutputFormat;

      public class DTDComments {
      public static void main(String[] args) throws Exception {
                  String data =
                      "<?xml version=\"1.0\" ?>" +
                      "<!DOCTYPE root [" +
                      "<!ELEMENT root ANY>" +
                      "<!-- DTD comment -->" +
                      "]>" +
                      "<root><!-- comment in instance --><child>data</child></root>";

      ByteArrayInputStream in = new ByteArrayInputStream(data.getBytes());
      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
      //dbf.setIgnoringComments(true);
      DocumentBuilder db = dbf.newDocumentBuilder();
      Document doc = db.parse(in);
      Node node = doc.getDocumentElement();
      OutputFormat format = new OutputFormat((Document)doc);
      XMLSerializer ser = new XMLSerializer(System.out, format);
      System.out.println("********Serialize********");
      ser.serialize((Document)doc);
      System.out.println("\n\nDone");
      }
      }
      ---------------------------------------------------------------
      Output:
      ********Serialize********
      <?xml version="1.0" encoding="UTF-8"?>
      <!DOCTYPE root [<!ELEMENT root ANY>
      ]>
      <!-- DTD comment --><root><!-- comment in instance --><child>data</child></root>

      Done
      ---------------------------------------------------------------
      ###@###.###
      19-April-2002

            duke J. Duke
            duke J. Duke
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: