You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xerces.apache.org by Jean Georges PERRIN <jg...@jgp.net> on 2003/01/13 18:09:16 UTC

Cloning a tree

Hi,

I'd like to clone a Document and all its subnodes. Is there a quick method
to do so or do I need to do it manual or even to reparse the whole tree?

I'd like something like:
Document myDoc = mySourceDoc.clone();

myDoc could be modified in any way without altering mySourceDoc. Of course,
the API could be named differently or it could go through a Builder...

Thanks for any help...

Jean Georges PERRIN



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


Re: Cloning a tree

Posted by Brian Madigan <bu...@yahoo.com>.
Hi,
This might not be the cleanest or best way, but I
sometimes need to create new Documents from fragments
other documents, the operation is pretty much the same
as what you need:
//first create an empty document:
        DocumentBuilderFactory dbf =     
DocumentBuilderFactory.newInstance()        
Document clone = dbf.newDocumentBuilder(
).newDocument( );
// import the document element or the node you are
cloning
clone.importNode(docToClone.getDocumentElement( ),
true);
clone.appendChile(docToClone.getDocumentElement( ));


--- Jean Georges PERRIN <jg...@jgp.net> wrote:
> Hi,
> 
> I'd like to clone a Document and all its subnodes.
> Is there a quick method
> to do so or do I need to do it manual or even to
> reparse the whole tree?
> 
> I'd like something like:
> Document myDoc = mySourceDoc.clone();
> 
> myDoc could be modified in any way without altering
> mySourceDoc. Of course,
> the API could be named differently or it could go
> through a Builder...
> 
> Thanks for any help...
> 
> Jean Georges PERRIN
> 
> 
> 
>
---------------------------------------------------------------------
> To unsubscribe, e-mail:
> xerces-j-user-unsubscribe@xml.apache.org
> For additional commands, e-mail:
> xerces-j-user-help@xml.apache.org
> 


__________________________________________________
Do you Yahoo!?
Yahoo! Mail Plus - Powerful. Affordable. Sign up now.
http://mailplus.yahoo.com

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


Re: Cloning a tree

Posted by Joseph Kesselman <ke...@us.ibm.com>.
See documentation for the DOM Level 2 Node.cloneNode() method. But note 
that this is not guaranteed to work on Document nodes; in some 
implementations, you may have to create a new Document, and then use the 
DOM Level 2 importNode() method to copy the children of the old Document 
into the new one... so see the docs for that too. (See also 
http://www.w3.org/DOM/faq.html#ownerdoc for discussion of why importNode 
is different from cloneNode.)

If you're writing DOM Level 1 code, then yes, you must provide your own 
code to walk and copy the tree.

______________________________________
Joe Kesselman  / IBM Research


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