File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes XML Gurus. Deep cloning a node without using clone(true) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "XML Gurus. Deep cloning a node without using clone(true)" Watch "XML Gurus. Deep cloning a node without using clone(true)" New topic
Author

XML Gurus. Deep cloning a node without using clone(true)

Francesco Marchioni
author
Ranch Hand

Joined: Sep 22, 2003
Posts: 190
Dear all,
I have to copy one piece of XML from one part of the document to another. Unfortunately I cannot simply do a copy and paste:

Node nodeLeaf = nodeChild.cloneNode(true);
nodeAncestor.appendChild(nodeLeaf);

because some fragments of the nodeLeaf mustn't be copied (depending on the node value)

So I cannot make a deep clone of the leaf.
I have to make a clone of nodeLeaf by myself. (Copying its child with a recursive function)

I have built a recursive function but I cannot understand why it doesn't work correclty (text values are skipped and also some pieces of the xml aren't copied correclty)



Anybody can give me a help ?
thanks a lot
Francesco


WildFly 8 Administration Book - JBoss Tutorials
Rajagopal Manohar
Ranch Hand

Joined: Nov 26, 2004
Posts: 183
text values are skipped and also some pieces of the xml aren't copied correclty



node.getChildNodes();will return only the child nodes (kids ) not descendents (grand children and above).

Text nodes are usually children of element nodes in which they are contained unless they are whitespaces around the element.

See if you can work out a new recursive solution factoring in what I have said
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: XML Gurus. Deep cloning a node without using clone(true)