Java的XML附加文档现有文档 [英] Java appending XML docs to existing docs

查看:165
本文介绍了Java的XML附加文档现有文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我创建两个XML文档,我想这两个里面一个新的信封结合起来。所以,我有

I have two XML docs that I've created and I want to combine these two inside of a new envelope. So I have

<alert-set>
  <warning>National Weather Service...</warning>
  <start-date>5/19/2009</start-date>
  <end-date>5/19/2009</end-date>
</alert-set>

 <weather-set>
   <chance-of-rain type="percent">31</chance-of-rain>
   <conditions>Partly Cloudy</conditions>
   <temperature type="Fahrenheit">78</temperature>
 </weather-set>

我想要做的就是将二者结合起来根节点内:其中;数据集>合并文档&LT; /数据集>

What I'd like to do is combine the two inside a root node: < DataSet> combined docs < /DataSet>

我试图创建一个临时文档和文件的根节点替换孩子:

I've tried creating a temporary doc and replacing children with the root nodes of the documents:

<DataSet>
  <blank/>
  <blank/>
</DataSet>

和我希望用这两个文件的根元素替换两个空白,但我得到WRONG_DO​​CUMENT_ERR:一个节点在不同的文档而不是创建它的人使用我尝试采用和导入根节点,但我得到了同样的错误。

And I was hoping to replace the two blanks with the root elements of the two documents but I get "WRONG_DOCUMENT_ERR: A node is used in a different document than the one that created it." I tried adopting and importing the root nodes but I get the same error.

请问有没有无需通读并为每个节点创建新的元素组合文档的一些简单的方法是什么?

Is there not some easy way of combining documents without having to read through and create new elements for each node?

编辑:样品code片段
只是试图移动一个空白的文件现在...的importNode和adoptNode功能无法导入/采用文档节点,但他们不能导入元素节点和它的子树...或者,如果是这样,它确实似乎不为附加/更换仍然工作。

Sample code snippets Just trying to move one to the "blank" document for now... The importNode and adoptNode functions cannot import/adopt Document nodes, but they can't import the element node and its subtree... or if it does, it does not seem to work for appending/replacing still.

	Document xmlDoc;     //created elsewhere
	Document weather = getWeather(latitude, longitude);
	Element weatherRoot = weather.getDocumentElement();

	Node root = xmlDoc.getDocumentElement();
	Node adopt = weather.adoptNode(weatherRoot);
	Node imported = weather.importNode(weatherRoot, true);
	Node child = root.getFirstChild();

	root.replaceChild(adopt, child);      //initially tried replacing the <blank/> elements
	root.replaceChild(imported, child);

	root.appendChild(adopt);
	root.appendChild(imported);
	root.appendChild(adopt.cloneNode(true));

所有这些抛出抛出:DOMException:WRONG_DO​​CUMENT_ERR:一个节点在不同的文档而不是创建它的人使用。

All of these throw the DOMException: WRONG_DOCUMENT_ERR: A node is used in a different document than the one that created it.

我想我要弄清楚如何使用STAX或只是重新读取文件,并创建新的元素......这有点儿似乎有太多的工作,只是为了文档合并,虽然。

I think I'll have to figure out how to use stax or just reread the documents and create new elements... That kinda seems like too much work just to combine documents, though.

推荐答案

这是一个有点棘手,但下面的示例运行:

It's a bit tricky, but the following example runs:

public static void main(String[] args) {

	DocumentImpl doc1 = new DocumentImpl();
	Element root1 = doc1.createElement("root1");
	Element node1 = doc1.createElement("node1");
	doc1.appendChild(root1);
	root1.appendChild(node1);

	DocumentImpl doc2 = new DocumentImpl();
	Element root2 = doc2.createElement("root2");
	Element node2 = doc2.createElement("node2");
	doc2.appendChild(root2);
	root2.appendChild(node2);

	DocumentImpl doc3 = new DocumentImpl();
	Element root3 = doc3.createElement("root3");
	doc3.appendChild(root3);

	// root3.appendChild(root1); // Doesn't work -> DOMException
	root3.appendChild(doc3.importNode(root1, true));

	// root3.appendChild(root2); // Doesn't work -> DOMException
	root3.appendChild(doc3.importNode(root2, true));   
}

这篇关于Java的XML附加文档现有文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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