18

我创建了两个 XML 文档,我想将这两个文档合并到一个新信封中。所以我有

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

我想做的是在根节点内将两者结合起来: <DataSet> 结合文档 < /DataSet>

我尝试创建一个临时文档并用文档的根节点替换子节点:

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

我希望用两个文档的根元素替换这两个空白,但我得到“WRONG_DOCUMENT_ERR:一个节点在与创建它的文档不同的文档中使用。” 我尝试采用和导入根节点,但我得到了同样的错误。

是否有一些简单的方法可以组合文档而无需通读并为每个节点创建新元素?

编辑:示例代码片段现在只是试图将一个移动到“空白”文档...... importNode 和adoptNode 函数无法导入/采用文档节点,但它们不能导入元素节点及其子树......或者如果确实如此,它似乎仍然不适用于附加/替换。

    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:一个节点在与创建它的文档不同的文档中使用。

我想我必须弄清楚如何使用 stax 或者只是重新阅读文档并创建新元素......不过,这似乎有点像组合文档的工作量太大。

4

2 回答 2

29

这有点棘手,但以下示例运行:

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));   
}
于 2009-05-19T18:31:29.943 回答
7

我知道你已经解决了这个问题,但我仍然想使用我目前正在测试的XOM库(与这个问题相关)来解决这个问题,并且在这样做的同时,提供一种不同于Andreas_D 的回答。

(为了简化这个例子,我将你的<alert-set>and<weather-set>放入单独的文件中,我将这些文件读入nu.xom.Document实例。)

import nu.xom.*;

[...]

Builder builder = new Builder();
Document alertDoc = builder.build(new File("src/xomtest", "alertset.xml"));
Document weatherDoc = builder.build(new File("src/xomtest", "weatherset.xml"));
Document mainDoc = builder.build("<DataSet><blank/><blank/></DataSet>", "");

Element root = mainDoc.getRootElement();
root.replaceChild(
    root.getFirstChildElement("blank"), alertDoc.getRootElement().copy());
root.replaceChild(
    root.getFirstChildElement("blank"), weatherDoc.getRootElement().copy());

关键是复制要插入的元素mainDoc;否则你会收到“孩子已经有父母”的抱怨。

输出 mainDoc 现在给出:

<?xml version="1.0" encoding="UTF-8"?>
<DataSet>
    <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>
</DataSet>

令我高兴的是,这对 XOM 来说非常简单。写这篇文章只花了几分钟,尽管我对这个库还不是很有经验。(如果没有这些<blank/>元素会更容易,即从简单的 . 开始<DataSet></DataSet>。)

因此,除非您有令人信服的理由只使用标准 JDK 工具,否则我强烈建议您尝试 XOM,因为它可以使 Java 中的 XML 处理更加愉快。

于 2009-06-08T20:38:14.253 回答