如何将一个xml文档的节点复制到另一个?

时间:2023-01-27 11:16:56

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

我试图在java中将一个元素节点(有一些子节点)从一个XML插入到另一个XML中。

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?

有没有替代方法,没有通过doc1节点递归并在doc2中手动创建整个事物?

1 个解决方案

#1


8  

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! :)

document.importNode()做得很好....谢谢我! :)

#1


8  

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! :)

document.importNode()做得很好....谢谢我! :)