For my case, I need to take some nodes from the existing XML file and create a new document and add the all nodes that are in existing file.
对于我的情况,我需要从现有的XML文件中获取一些节点并创建一个新文档并添加现有文件中的所有节点。
How do I add element from an existing document to a new document?
如何将现有文档中的元素添加到新文档?
1 个解决方案
#1
0
Likely problem is that you tried to insert node to the target document without importing it. Importing node to target document is done via Document.importNode. Code is roughly as follows, of course exact location in target document should be changed specific to you application:
可能的问题是您尝试将节点插入目标文档而不导入它。将节点导入目标文档是通过Document.importNode完成的。代码大致如下,当然,目标文档中的确切位置应根据您的应用程序进行更改:
NodeList list ...
for(int i=0; i < list.getLength(); i++){
Node nodeToImport = list.item(i);
Node importedNode = targetDocument.importNode(nodeToImport, true);
targetDocument.getDocumentElement().appendChild(importedNode);
}
#1
0
Likely problem is that you tried to insert node to the target document without importing it. Importing node to target document is done via Document.importNode. Code is roughly as follows, of course exact location in target document should be changed specific to you application:
可能的问题是您尝试将节点插入目标文档而不导入它。将节点导入目标文档是通过Document.importNode完成的。代码大致如下,当然,目标文档中的确切位置应根据您的应用程序进行更改:
NodeList list ...
for(int i=0; i < list.getLength(); i++){
Node nodeToImport = list.item(i);
Node importedNode = targetDocument.importNode(nodeToImport, true);
targetDocument.getDocumentElement().appendChild(importedNode);
}