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 <ha...@swbell.net> on 2000/03/01 01:19:50 UTC

Another question about using Xerces

I'm trying to build a document using Xerces.  Here's some simple code that demonstrates my problem:

  public XercesTest() {
    DOMImplementationImpl imp = new DOMImplementationImpl();
    Document doc = imp.createDocument(null, "Test", null);
    Element e = doc.createElement("dummElement");
    doc.appendChild(e);
  }

Here's the error I receive:
org.apache.xerces.dom.DOMExceptionImpl: HIERARCHY_REQUEST_ERR
 at org.apache.xerces.dom.DocumentImpl.insertBefore(Compiled Code)
 at org.apache.xerces.dom.NodeImpl.appendChild(Compiled Code)
 at XercesTest.XercesTest.<init>(XercesTest.java:19)
 at XercesTest.XercesTest.main(XercesTest.java:23)

What's a HIERARCHY_REQUEST_ERR and where is it documented?
What's the correct way to add a new element to a document?

Thanks.


Re: Another question about using Xerces

Posted by Arnaud Le Hors <le...@us.ibm.com>.
Eric Hodges wrote:
> 
> Ah, thanks.  I've never seen that spec before, and I've been to W3.org many
> times.

The place to look for W3C specs is http://www.w3.org/TR
TR stands for Technical Reports. The DOM Level 2 spec is listed there.

> Does anyone know why Document has an appendChild method if it can't have
> children?  Is it just a bad interface hierarchy?

While you can't add any other Element node to a Document you could, for
instance, add a Comment node.
-- 
Arnaud  Le Hors - IBM Cupertino, XML Technology Group

Re: Another question about using Xerces

Posted by Eric Hodges <ha...@swbell.net>.
Ah, thanks.  I've never seen that spec before, and I've been to W3.org many
times.

Does anyone know why Document has an appendChild method if it can't have
children?  Is it just a bad interface hierarchy?


----- Original Message -----
From: Arnaud Le Hors <le...@us.ibm.com>
To: <ge...@xml.apache.org>
Sent: Wednesday, March 01, 2000 12:02 PM
Subject: Re: Another question about using Xerces


> As defined in the spec [1] createDocument() not only create the Document
> node but also the root element (also called "document element"). So you
> need to start adding elements to the root element and not to the
> Document itself. Try:
>
>  public XercesTest() {
>     DOMImplementationImpl imp = new DOMImplementationImpl();
>     Document doc = imp.createDocument(null, "Test", null);
>     Element e = doc.createElement("dummElement");
>     doc.getDocumentElement().appendChild(e);
>   }
>
> Since a Document can only have one element child, trying to add any
> other raises an exception.
>
> [1]
>
http://www.w3.org/TR/1999/CR-DOM-Level-2-19991210/core.html#Level-2-Core-DOM
-createDocument
> --
> Arnaud  Le Hors - IBM Cupertino, XML Technology Group


Re: Another question about using Xerces

Posted by Arnaud Le Hors <le...@us.ibm.com>.
As defined in the spec [1] createDocument() not only create the Document
node but also the root element (also called "document element"). So you
need to start adding elements to the root element and not to the
Document itself. Try:

 public XercesTest() {
    DOMImplementationImpl imp = new DOMImplementationImpl();
    Document doc = imp.createDocument(null, "Test", null);
    Element e = doc.createElement("dummElement");
    doc.getDocumentElement().appendChild(e);
  }

Since a Document can only have one element child, trying to add any
other raises an exception.

[1]
http://www.w3.org/TR/1999/CR-DOM-Level-2-19991210/core.html#Level-2-Core-DOM-createDocument
-- 
Arnaud  Le Hors - IBM Cupertino, XML Technology Group

Re: Another question about using Xerces

Posted by Eric Hodges <ha...@swbell.net>.
I stepped into the source before I posted here.  This is the source, from
org.apache.xerces.dom.DocumentImpl:

    /**
     * Since a Document may contain at most one top-level Element child,
    * and at most one DocumentType declaraction, we need to subclass our
    * add-children methods to implement this constraint.
  * Since appendChild() is implemented as insertBefore(,null),
  * altering the latter fixes both.
    * <p>
    * While I'm doing so, I've taken advantage of the opportunity to
    * cache documentElement and docType so we don't have to
    * search for them.
  */
    public Node insertBefore(Node newChild, Node refChild)
        throws DOMException {

     // Only one such child permitted
        int type = newChild.getNodeType();
        if (errorChecking) {
            if((type == Node.ELEMENT_NODE && docElement != null) ||
               (type == Node.DOCUMENT_TYPE_NODE && docType != null)) {
                throw new
DOMExceptionImpl(DOMException.HIERARCHY_REQUEST_ERR,
                                           "HIERARCHY_REQUEST_ERR");
            }
        }

I'm not on the Xerces list, and this code didn't make any sense to me.  I
have no idea what docElement is and why it needs to be null.


----- Original Message -----
From: Mike Pogue <mp...@apache.org>
To: <ge...@xml.apache.org>
Sent: Wednesday, March 01, 2000 11:21 AM
Subject: Re: Another question about using Xerces


> Eric,
>
> You should post this to the Xerces list.  But, here's the quick answer:
> HIERARCHY_REQUEST_ERR is documented in the XML specification, at:
>
> http://www.w3.org/TR/REC-DOM-Level-1/level-one-core.html
>
> If you debug into the source code, where the exception is thrown, it will
probably
> give you a lot of info about where the problem is.
>
> Mike
>
> > Eric Hodges wrote:
> >
> > I'm trying to build a document using Xerces.  Here's some simple code
that demonstrates my
> > problem:
> >
> >   public XercesTest() {
> >     DOMImplementationImpl imp = new DOMImplementationImpl();
> >     Document doc = imp.createDocument(null, "Test", null);
> >     Element e = doc.createElement("dummElement");
> >     doc.appendChild(e);
> >   }
> >
> > Here's the error I receive:
> > org.apache.xerces.dom.DOMExceptionImpl: HIERARCHY_REQUEST_ERR
> >  at org.apache.xerces.dom.DocumentImpl.insertBefore(Compiled Code)
> >  at org.apache.xerces.dom.NodeImpl.appendChild(Compiled Code)
> >  at XercesTest.XercesTest.<init>(XercesTest.java:19)
> >  at XercesTest.XercesTest.main(XercesTest.java:23)
> > What's a HIERARCHY_REQUEST_ERR and where is it documented?
> > What's the correct way to add a new element to a document?
> >
> > Thanks.
> >


Re: Another question about using Xerces

Posted by Mike Pogue <mp...@apache.org>.
Eric,

	You should post this to the Xerces list.  But, here's the quick answer:
HIERARCHY_REQUEST_ERR is documented in the XML specification, at:

	http://www.w3.org/TR/REC-DOM-Level-1/level-one-core.html

If you debug into the source code, where the exception is thrown, it will probably
give you a lot of info about where the problem is.

Mike

> Eric Hodges wrote:
> 
> I'm trying to build a document using Xerces.  Here's some simple code that demonstrates my
> problem:
> 
>   public XercesTest() {
>     DOMImplementationImpl imp = new DOMImplementationImpl();
>     Document doc = imp.createDocument(null, "Test", null);
>     Element e = doc.createElement("dummElement");
>     doc.appendChild(e);
>   }
> 
> Here's the error I receive:
> org.apache.xerces.dom.DOMExceptionImpl: HIERARCHY_REQUEST_ERR
>  at org.apache.xerces.dom.DocumentImpl.insertBefore(Compiled Code)
>  at org.apache.xerces.dom.NodeImpl.appendChild(Compiled Code)
>  at XercesTest.XercesTest.<init>(XercesTest.java:19)
>  at XercesTest.XercesTest.main(XercesTest.java:23)
> What's a HIERARCHY_REQUEST_ERR and where is it documented?
> What's the correct way to add a new element to a document?
> 
> Thanks.
>