F
fmarchioni
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)
// Passing the full leaf and a leaf with only the first tag, under
which I clone manually the nodes.
Node copyNode = appendNodes(nodeLeaf, nodeLeaf.cloneNode(false));
private Node appendNodes(Node node, Node copyNode) {
NodeList kids = node.getChildNodes();
int length = kids.getLength();
if (kids != null) {
for (int ii = 0; ii < length; ii++) {
Node nodeKid = kids.item(ii);
copyNode.appendChild(nodeKid.cloneNode(false));
appendNodes(nodeKid,nodeKid);
}
}
return copyNode;
}
Anybody can give me a help ?
thanks a lot
Francesco
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)
// Passing the full leaf and a leaf with only the first tag, under
which I clone manually the nodes.
Node copyNode = appendNodes(nodeLeaf, nodeLeaf.cloneNode(false));
private Node appendNodes(Node node, Node copyNode) {
NodeList kids = node.getChildNodes();
int length = kids.getLength();
if (kids != null) {
for (int ii = 0; ii < length; ii++) {
Node nodeKid = kids.item(ii);
copyNode.appendChild(nodeKid.cloneNode(false));
appendNodes(nodeKid,nodeKid);
}
}
return copyNode;
}
Anybody can give me a help ?
thanks a lot
Francesco