Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how does one go about copying one xml document's node to another?

Tags:

java

xml

I'm trying to insert an element node (that has some children) from one XML into another in java.

What I'm trying (which isn't working) looks like this...

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

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

I'm getting an exception that complains that i'm trying to use a node created by one document in another.

Is there an alternative, short of recursing through doc1's node and creating the whole thing manually in doc2?

like image 897
Yevgeny Simkin Avatar asked Mar 06 '09 18:03

Yevgeny Simkin


1 Answers

I hate asking questions, thinking I've hit a wall, and then suddenly just stumbling on the answer that was there in front of me the whole time!

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

like image 197
Yevgeny Simkin Avatar answered Nov 14 '22 22:11

Yevgeny Simkin