java XML:将 xml 文档附加到另一个文档的节点中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4613140/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
XML: to append xml document into the node of another document
提问by Bibhaw
I have to insert file1.xml elements into another file2.xml. file2.xml has several node and each node has it's node_id. is there any way to do that.
我必须将 file1.xml 元素插入另一个 file2.xml。file2.xml 有几个节点,每个节点都有它的 node_id。有没有办法做到这一点。
let suppose :
让假设:
file1.xml :
文件 1.xml :
< root>
<node_1>......</node_1>
</root>
file2.xml :
文件 2.xml :
< root>
< node>
< node_id>1'<'/node_id>
< /node>
< /root>
I want ? file2.xml :
我想要 ?文件 2.xml :
< root>
< node>
<node_1>......</node_1> [here i want to append the file1.xml]
</node>
</root>
回答by dogbane
- Iterate over all the node_id elements in file2.
- For each one, look up corresponding node_x element in file1.
- Add node_x from file1 into file2
- 迭代 file2 中的所有 node_id 元素。
- 对于每一个,在 file1 中查找对应的 node_x 元素。
- 将文件 1 中的 node_x 添加到文件 2
The following code illustrates this:
以下代码说明了这一点:
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
//build DOMs
Document doc1 = builder.parse(new File("file1.xml"));
Document doc2 = builder.parse(new File("file2.xml"));
//get all node_ids from doc2 and iterate
NodeList list = doc2.getElementsByTagName("node_id");
for(int i = 0 ; i< list.getLength() ; i++){
Node n = list.item(i);
//extract the id
String id = n.getTextContent();
//now get all node_id elements from doc1
NodeList list2 = doc1.getElementsByTagName("node_"+id);
for(int j = 0 ; j< list2.getLength() ; j++){
Node m = list2.item(j);
//import them into doc2
Node imp = doc2.importNode(m,true);
n.getParent().appendChild(imp);
}
}
//write out the modified document to a new file
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
Source source = new DOMSource(doc2);
Result output = new StreamResult(new File("merged.xml"));
transformer.transform(source, output);
The result would be:
结果将是:
<root>
<node>
<node_id>1</node_id>
<node_1>This is 1</node_1>
</node>
<node>
<node_id>2</node_id>
<node_2>This is 2</node_2>
</node>
<node>
<node_id>3</node_id>
<node_3>This is 3</node_3>
</node>
</root>
回答by Andreas Dolk
Usual approach:
通常的做法:
parse both documents from file1 and file2 into Document
objects (SAXParser, jDom, dom4j), then importelement <node_1>
from the first document to the second and add it to <node>
. Then delete the corresponding <node_id>
element.
将 file1 和 file2 中的两个文档解析为Document
对象(SAXParser、jDom、dom4j),然后将元素<node_1>
从第一个文档导入到第二个文档并将其添加到<node>
. 然后删除相应的<node_id>
元素。
Importingis necessary, the Document
implementations offer the correct methods for this process! Just adding an element from one document to another documents will result in DOMExceptions
.
导入是必要的,Document
实现为这个过程提供了正确的方法!仅将一个文档中的元素添加到另一个文档将导致DOMExceptions
.