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 Peter Wilkins <pe...@cmis.csiro.au> on 2000/06/20 07:40:55 UTC

insertBefore method

Hi all,

    First off my admission to being a newbie.  My apologies if this has
been covered before but I have not been able to find any reference to
it.  Does anyone know how to correctly use the insertBefore method?
Each time I have tried to use it to insert an element into a document, I
get a

DOM008

error at runtime (although code compiles fine).  I have tried several
different permutations of ways to call this method and none seem to
work.

This has left me to resort to using appendChild which does not put the
elements into the XML store at the position I want.

Any help greately appreciated,

Pete




RE: insertBefore method

Posted by Jean-Louis Vila <jl...@cosmosbay.com>.
Hi Pete,

I see, you create an Element in testing !
I didn't see why an exception is raised but
I can give you some things to try !

1 - Are you use Namespace, if yes, you have to use
    createElementNS

2- Perhaps, createElement considers the new Element
   independant from Document object (bug or !bug ?)

3 - Just try
   Node newNode=testing.importNode(myElement,true);
   test.insertBefore(newNode, (Element)test);

hope that helps,
Jean-Louis Vila

-----Message d'origine-----
De : Peter Wilkins [mailto:peter.wilkins@cmis.csiro.au]
Envoye : mardi 20 juin 2000 09:28
A : xerces-j-dev@xml.apache.org
Objet : Re: insertBefore method


Ahhh, kinda helps except that unfortunately I am getting the nodes from the
same document, perhaps to help clarify here is the code that I was using:

      String xmlFile = "sample.xml";

      Document testing = XMLUtils.parseDoc(xmlFile);

      // code for inserting a new node

      Element myElement = testing.createElement("meetingItem");
      myElement.setAttribute("meetingItemID", "NewValue");
      Element subMyElem = testing.createElement("descript");
      Element subSubmyElem = testing.createElement("detail");
      Text info = testing.createTextNode("Here is some new data");

      subSubmyElem.appendChild(info);
      subMyElem.appendChild(subSubmyElem);
      myElement.appendChild(subMyElem);

      NodeList ml = testing.getElementsByTagName("agenda");
      Node t2 = ml.item(0);
      NodeList myList = testing.getElementsByTagName("actionItem");
      Node test = myList.item(0);
      test.insertBefore((Element)myElement, (Element)test);

I know the creation of the new node works as I have been able to append it
to
the end of the xml file correctly with serialization.

Again thanks for the help,

Pete

Jean-Louis Vila wrote:

> Hi Pete,
>
> You can only insertBefore in case of all Node are in the same
> document!
>
> Typically, to get nodes from another document, you couldn't
> use insertBefore directly because of DOM008 Exception
(WRONG_DOCUMENT_ERR).
>
> You have to import the node (Docuemnt.importNode()) and
> insert it in the current document (insertBefore()).
> Hope that help
> Jean-Louis Vila
>
> >    First off my admission to being a newbie.  My apologies if this has
> >been covered before but I have not been able to find any reference to
> >it.  Does anyone know how to correctly use the insertBefore method?
> >Each time I have tried to use it to insert an element into a document, I
> >get a
>
> >DOM008
>
> >error at runtime (although code compiles fine).  I have tried several
> >different permutations of ways to call this method and none seem to
> >work.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-j-dev-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-j-dev-help@xml.apache.org
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-j-dev-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-j-dev-help@xml.apache.org




---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-dev-help@xml.apache.org


Re: insertBefore method

Posted by Peter Wilkins <pe...@cmis.csiro.au>.
Ahhh, kinda helps except that unfortunately I am getting the nodes from the
same document, perhaps to help clarify here is the code that I was using:

      String xmlFile = "sample.xml";

      Document testing = XMLUtils.parseDoc(xmlFile);

      // code for inserting a new node

      Element myElement = testing.createElement("meetingItem");
      myElement.setAttribute("meetingItemID", "NewValue");
      Element subMyElem = testing.createElement("descript");
      Element subSubmyElem = testing.createElement("detail");
      Text info = testing.createTextNode("Here is some new data");

      subSubmyElem.appendChild(info);
      subMyElem.appendChild(subSubmyElem);
      myElement.appendChild(subMyElem);

      NodeList ml = testing.getElementsByTagName("agenda");
      Node t2 = ml.item(0);
      NodeList myList = testing.getElementsByTagName("actionItem");
      Node test = myList.item(0);
      test.insertBefore((Element)myElement, (Element)test);

I know the creation of the new node works as I have been able to append it to
the end of the xml file correctly with serialization.

Again thanks for the help,

Pete

Jean-Louis Vila wrote:

> Hi Pete,
>
> You can only insertBefore in case of all Node are in the same
> document!
>
> Typically, to get nodes from another document, you couldn't
> use insertBefore directly because of DOM008 Exception (WRONG_DOCUMENT_ERR).
>
> You have to import the node (Docuemnt.importNode()) and
> insert it in the current document (insertBefore()).
> Hope that help
> Jean-Louis Vila
>
> >    First off my admission to being a newbie.  My apologies if this has
> >been covered before but I have not been able to find any reference to
> >it.  Does anyone know how to correctly use the insertBefore method?
> >Each time I have tried to use it to insert an element into a document, I
> >get a
>
> >DOM008
>
> >error at runtime (although code compiles fine).  I have tried several
> >different permutations of ways to call this method and none seem to
> >work.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-j-dev-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-j-dev-help@xml.apache.org
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-j-dev-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-j-dev-help@xml.apache.org




RE: insertBefore method

Posted by Jean-Louis Vila <jl...@cosmosbay.com>.
Hi Pete,

You can only insertBefore in case of all Node are in the same
document!

Typically, to get nodes from another document, you couldn't
use insertBefore directly because of DOM008 Exception (WRONG_DOCUMENT_ERR).

You have to import the node (Docuemnt.importNode()) and
insert it in the current document (insertBefore()).
Hope that help
Jean-Louis Vila

>    First off my admission to being a newbie.  My apologies if this has
>been covered before but I have not been able to find any reference to
>it.  Does anyone know how to correctly use the insertBefore method?
>Each time I have tried to use it to insert an element into a document, I
>get a

>DOM008

>error at runtime (although code compiles fine).  I have tried several
>different permutations of ways to call this method and none seem to
>work.



---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-dev-help@xml.apache.org