如何将一个 xml 文档的节点复制到另一个? [英] how does one go about copying one xml document's node to another?

查看:48
本文介绍了如何将一个 xml 文档的节点复制到另一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一个元素节点(有一些子节点)从一个 XML 插入到另一个 Java 中.

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.

除了通过 doc1 的节点递归并在 doc2 中手动创建整个内容之外,还有其他选择吗?

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

推荐答案

我讨厌提出问题,以为自己碰壁了,然后突然就绊倒了一直摆在我面前的答案!

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() 很好地完成了这个技巧......谢谢我!:)

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

这篇关于如何将一个 xml 文档的节点复制到另一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆