Re: HRE when adding to Doc created with DTD

>I create my Document object like this:
>    ruledoc = new DocumentImpl();

You're probably aware of this, but that constructor is not portable --
you're explicitly accessing a specific implementation of the DOM, and you
then have to add the root element which is a slight violation of the DOM
architecture. It's better to use the DOMImplementation.createDocument()
call, which is portable and creates the Document and the root element
simultaneously. (Obtaining the DOMImplementation itself is still a
nonportable step, but that's a one-time operation.)

>rule.getOwnerDocument().getFirstChild().appendChild(rule);

The first child of the Document is _NOT_ necessarily the root element.
If you're inadvertently trying to append rule to a comment, or to the
DocumentType node, the DOM will quite correctly tell you that this is not
acceptable. The fact that validation affects this suggests that it is the
DocumentType that's surprising you.

If you really want to do so, you can check this by printing out
     rule.getOwnerDocument().getFirstChild().getNodeType()
but simply switching to
     rule.getOwnerDocument().getDocumentElement().appendChild(rule);
should fix the problem.


______________________________________
Joe Kesselman  / IBM Research

Received on Wednesday, 4 April 2001 09:24:00 UTC