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 Just van den Broecke <ju...@justobjects.nl> on 2000/08/04 22:48:04 UTC

Moving Elements between Documents raises WRONG_DOCUMENT_ERR

Hi,

I just moved from IBM xml4j 2.0.15 to Apache XML xerces-j 1.0.3 (because
I moved from Lotus to Xalan XSLT 1.1D01). Some of my custom DOM
processing code that moved Elements between two Documents now suddenly
raises a WRONG_DOCUMENT_ERR in Node.replaceChild() with xerces (not with
xml4j). I tried importing the Element to be moved in the new Document
since the new element belongs to a different node. I tried cloning and
removing the element but each time I get the exception. My code now
looks something like:

oldElement.getOwnerDocument().importNode(newElement, true);
parent.replaceChild(newElement, oldElement); --> raises
WRONG_DOCUMENT_ERR

newElement comes from a different Document (and DTD) that oldElement. 
Any ideas ? Thanks,

--Just

Just van den Broecke          just@justobjects.nl
Just Objects B.V.             tel. +31 65 4268627                
The Netherlands               http://www.justobjects.nl

RE: Moving Elements between Documents raises WRONG_DOCUMENT_ERR

Posted by Lawrence Fry <lw...@bbnow.net>.
Just,

I had the same problem. The replaceChild only works if both children have
the same document parent. I believe the parent of newelement in importNode
is immutable...but the returned node has the proper reference.   The
importNode documentation says it doesn't work...but it does if you execute
the method below.

Regards,

Lawrence

public Element replaceElement(Element newelement, Element oldelement){

     Document doc = oldElement.getOwnerDocument();

     Node node = doc.importNode((Node)newelement,true);

     Element element = (Element)node;


      //  remove the old element

       doc.getDocumentElement().removeChild((Node)oldelement);

      // execute a getChildren so that the nodelist is reset...

       doc.getChildNodes();

       return element;

}


-----Original Message-----
From: Just van den Broecke [mailto:just@justobjects.nl]
Sent: Friday, August 04, 2000 1:48 PM
To: xerces-j-dev@xml.apache.org
Subject: Moving Elements between Documents raises WRONG_DOCUMENT_ERR


Hi,

I just moved from IBM xml4j 2.0.15 to Apache XML xerces-j 1.0.3 (because
I moved from Lotus to Xalan XSLT 1.1D01). Some of my custom DOM
processing code that moved Elements between two Documents now suddenly
raises a WRONG_DOCUMENT_ERR in Node.replaceChild() with xerces (not with
xml4j). I tried importing the Element to be moved in the new Document
since the new element belongs to a different node. I tried cloning and
removing the element but each time I get the exception. My code now
looks something like:

oldElement.getOwnerDocument().importNode(newElement, true);
parent.replaceChild(newElement, oldElement); --> raises
WRONG_DOCUMENT_ERR

newElement comes from a different Document (and DTD) that oldElement.
Any ideas ? Thanks,

--Just

Just van den Broecke          just@justobjects.nl
Just Objects B.V.             tel. +31 65 4268627
The Netherlands               http://www.justobjects.nl

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