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

查看:32
本文介绍了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>

我想做的是将两者结合在一个根节点中:<数据集>组合文档

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_DOCUMENT_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?

示例代码片段现在只是想将一个移动到空白"文档...... 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_DOCUMENT_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天全站免登陆