Note that there are some explanatory texts on larger screens.

plurals
  1. POJava appending XML docs to existing docs
    primarykey
    data
    text
    <p>I have two XML docs that I've created and I want to combine these two inside of a new envelope. So I have</p> <pre><code>&lt;alert-set&gt; &lt;warning&gt;National Weather Service...&lt;/warning&gt; &lt;start-date&gt;5/19/2009&lt;/start-date&gt; &lt;end-date&gt;5/19/2009&lt;/end-date&gt; &lt;/alert-set&gt; </code></pre> <p>and</p> <pre><code> &lt;weather-set&gt; &lt;chance-of-rain type="percent"&gt;31&lt;/chance-of-rain&gt; &lt;conditions&gt;Partly Cloudy&lt;/conditions&gt; &lt;temperature type="Fahrenheit"&gt;78&lt;/temperature&gt; &lt;/weather-set&gt; </code></pre> <p>What I'd like to do is combine the two inside a root node: &lt; DataSet> combined docs &lt; /DataSet></p> <p>I've tried creating a temporary doc and replacing children with the root nodes of the documents:</p> <pre><code>&lt;DataSet&gt; &lt;blank/&gt; &lt;blank/&gt; &lt;/DataSet&gt; </code></pre> <p>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.</p> <p>Is there not some easy way of combining documents without having to read through and create new elements for each node?</p> <p>EDIT: 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.</p> <pre><code> 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 &lt;blank/&gt; elements root.replaceChild(imported, child); root.appendChild(adopt); root.appendChild(imported); root.appendChild(adopt.cloneNode(true)); </code></pre> <p>All of these throw the DOMException: WRONG_DOCUMENT_ERR: A node is used in a different document than the one that created it.</p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload