You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-dev@xerces.apache.org by St...@r2isante.fr on 2000/10/11 08:43:45 UTC

Réf. : Extractingthe DOCTYPE from a DOM


Hi tim !!

I did the same mistake two weeks ago, so i'm happy to answer you today :=)

Your maistake is that you are serializing not the entire document but only
the root element by calling
xSer.serialize(doc.getDocumentElement());

In your case, the docuemnt have now two root élements which are the DTD
(processing instruction) and the root element.

So you have to use instaed :
xSer.serialize(doc);


Hope this helps..

Stéphane RAULT





"Downey, Tim" <td...@Tilion.com> le 10/10/2000 19:07:50

Veuillez répondre à xerces-j-dev@xml.apache.org

Pour :    xerces-j-dev@xml.apache.org
cc :  "Downey, Tim" <td...@Tilion.com>
Objet :   Extracting the DOCTYPE from a DOM



eSafe Protect Gateway (tm) a contrôlé ce mail.
Le ou les fichiers rattachés sont exempts de tout virus connus.

File: Tim Downey (E-mail). (203 bytes)
Encoding: 7bit
Result: Clean.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Hi gang,

This must have been asked before, but I can't seem to find any reference in
the archive.

I'm creating a document using DOM and then attempting to serialize it out
to
a stream for transmission.  I think I'm correctly setting the DOCTYPE, but
no matter what I do, I can't get the serialized form to contain the DOCTYPE
line.

Here's a snippet of the code that creates the document and the DOCTYPE

        DOMImplementation domImpl =
DOMImplementationImpl.getDOMImplementation();

        DocumentType docType = domImpl.createDocumentType("qualifiedName",
                                                          "publicId",
"systemId");

        Document doc = domImpl.createDocument("urn:art-org:art", "art",
                                              docType);

Here's where I serialize:

        try{
            OutputFormat format = new OutputFormat(doc);
            XMLSerializer xSer = new XMLSerializer(System.out,format);
            xSer.asDOMSerializer();
            xSer.serialize(doc.getDocumentElement());
        } catch (IOException i){ }

I've also tried using the DOMWriter sample code with no luck.  Is there any
way to extract the DOCTYPE from a DOM?

Thanks a lot
-tim


++++++++++++++++++++++++++++++++++++++++++++
Tim Downey                                                        Tilion,
Inc.
(978)461-4800 x243               5 Clock Tower Place, Suite 110
tdowney@tilion.com                              Maynard, MA 01754

 <<Tim Downey (E-mail).vcf>>

(See attached file: Tim Downey (E-mail).vcf)
---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-dev-help@xml.apache.org





Re: R�f. : Extractingthe DOCTYPE from a DOM

Posted by Jason Heath <jh...@iglou.com>.
Hello all,

  I'm wondering if there isn't a small bug in OutputFormat as well.
When I saw the Tim's email last night I went digging through the
code and found a couple of methods in OutputFormat whose bodies
were mostly commented out that would have allowed the DOCTYPE to
be set in the code that Tim originally showed. Based on the comments
in those methods I think these got overlooked when DOM Level 2
support was brought into the code.  Anyway, here is a diff from CVS
to show the code I'm talking about and how I would personally change
the code if it is indeed appropriate to do so.

Thanks,
Heath



Index: OutputFormat.java
===================================================================
RCS file:
/home/cvspublic/xml-xerces/java/src/org/apache/xml/serialize/OutputFormat.ja
va,v
retrieving revision 1.9
diff -c -r1.9 OutputFormat.java
*** OutputFormat.java 2000/08/30 18:59:21 1.9
--- OutputFormat.java 2000/10/11 15:00:08
***************
*** 880,899 ****
       */
      public static String whichDoctypePublic( Document doc )
      {
!         DocumentType doctype;
!
!         /* XXX  Delayed until DOM Level 2 is introduced into the code base
!            doctype = doc.getDoctype();
!            if ( doctype != null ) {
!            // Note on catch: DOM Level 1 does not specify this method
!            // and the code will throw a NoSuchMethodError
!            try {
!            return doctype.getPublicID();
!            } catch ( Error except ) {  }
!            }
!         */
!         if ( doc instanceof HTMLDocument )
              return DTD.XHTMLPublicId;
          return null;
      }

--- 880,894 ----
       */
      public static String whichDoctypePublic( Document doc )
      {
!         if ( doc instanceof HTMLDocument ) {
              return DTD.XHTMLPublicId;
+         }
+
+         DocumentType doctype = doc.getDoctype();
+         if ( doctype != null ) {
+            return doctype.getPublicId();
+         }
+
          return null;
      }

***************
*** 904,923 ****
       */
      public static String whichDoctypeSystem( Document doc )
      {
!         DocumentType doctype;
!
!         /* XXX  Delayed until DOM Level 2 is introduced into the code base
!            doctype = doc.getDoctype();
!            if ( doctype != null ) {
!            // Note on catch: DOM Level 1 does not specify this method
!            // and the code will throw a NoSuchMethodError
!            try {
!            return doctype.getSystemID();
!            } catch ( Error except ) { }
!            }
!         */
!         if ( doc instanceof HTMLDocument )
              return DTD.XHTMLSystemId;
          return null;
      }

--- 899,913 ----
       */
      public static String whichDoctypeSystem( Document doc )
      {
!         if ( doc instanceof HTMLDocument ) {
              return DTD.XHTMLSystemId;
+         }
+
+         DocumentType doctype = doc.getDoctype();
+         if ( doctype != null ) {
+             return doctype.getSystemId();
+         }
+
          return null;
      }