You are viewing a plain text version of this content. The canonical link for it is here.
Posted to general@xml.apache.org by Eric Hodges <er...@mongoosetech.com> on 2000/10/18 20:37:29 UTC

FW: Why is "#document" an illegal name?


-----Original Message-----
From: Eric Hodges [mailto:eric.hodges@mongoosetech.com]
Sent: Wednesday, October 18, 2000 11:59 AM
To: xerces-j-dev@xml.apache.org
Subject: Why is "#document" an illegal name?


I'm trying to create a document using
DOMImplementation.createDocument(namespaceURI, qualifiedName, docType).  I
get a "DOM002 illegal character" exception because the name I pass in is
"#document", which is the name DocumentImpl assigned the Document I'm trying
to recreate.

Any ideas?


Re: Why is "#document" an illegal name?

Posted by Eric Hodges <ha...@swbell.net>.
----- Original Message -----
From: "Sean Kelly" <ke...@ad1440.net>
To: <ge...@xml.apache.org>
Sent: Wednesday, October 18, 2000 6:03 PM
Subject: Re: Why is "#document" an illegal name?


> > I'm trying to create a document using
> > DOMImplementation.createDocument(namespaceURI, qualifiedName, docType).
I
> > get a "DOM002 illegal character" exception because the name I pass in is
> > "#document", which is the name DocumentImpl assigned the Document I'm
> trying
> > to recreate.
>
> You're passing "#document" as the qualifiedName parameter?  The # sign is
> illegal in an element name there.
>
> Pass in the name you want to give to the root element instead.

But I don't know the name of the root element when I create the document.
Why does createDocument want the name of the root element?  I don't need the
name of the root element when I create a document through JAXP.

Grrrrr.

I'm beginning to think it is impossible to write clean code that uses DOM.



Re: Why is "#document" an illegal name?

Posted by Sean Kelly <ke...@ad1440.net>.
> I'm trying to create a document using
> DOMImplementation.createDocument(namespaceURI, qualifiedName, docType).  I
> get a "DOM002 illegal character" exception because the name I pass in is
> "#document", which is the name DocumentImpl assigned the Document I'm
trying
> to recreate.

You're passing "#document" as the qualifiedName parameter?  The # sign is
illegal in an element name there.

Pass in the name you want to give to the root element instead.  For example,
suppose BoxCo (Silicon Valley's #1 Supplier of Brown 6-Sided Boxes, On The
Web at BoxCo.com, or call 1-800-BOX-CO) created box.dtd, containing a simple
DTD for describing boxes:

<!ELEMENT box (numSides, color)>
<!ELEMENT numSides (#PCDATA)>
<!ELEMENT color (#PCDATA)>

And you want to create a document that on paper looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE box "-//BoxCo//DTD Box 1.0//EN" "http://boxco.com/box.dtd">
<box>
  <numSides>6</numSides>
  <color>brown</color>
</box>

You'd create your document type

DocumentType type = impl.createDocumentType("box",
    "-//BoxCo//DTD Box 1.0//EN", http://boxco.com/box.dtd);

and create your document:

Document boxDoc = impl.createDocument(null, "box", type);

And get the <box> element:

Element box = boxDoc.getDocumentElement();

And add nodes to it:

Element numSides = boxDoc.createElement("numSides");
numSides.appendChild(boxDoc.createTextNode("6"));
box.appendChild(numSides);
Element color = boxDoc.createElement("color");
color.appendChild(boxdoc.createTextNode("brown"));
box.appendChild(color);

And there you have it.  (I probably made a mistake after too many glasses of
wine at lunch though, so beware.)

--Sean