How do I copy one XML node document to another?

I am trying to insert a node element (which has some child elements) from one XML to another in java.

What I'm trying (which doesn't work) looks like this:

Node foo = document1.getChildNodes().item(3).cloneNode(true);

document2.getChildNodes().item(2).appendChild(foo);

I get an exception that complains that I'm trying to use the node created by one document in another.

Is there an alternative not recursing through doc1 node and creating all this manually in doc2?

+5
source share
1 answer

I hate asking questions, thinking that I hit the wall, and then all of a sudden I just stumbled upon the answer that was there in front of me all the time!

document.importNode () does the trick nicely .... thanks to me! :)

+8
source

All Articles