Note that there are some explanatory texts on larger screens.

plurals
  1. POSplitting child node from XML file into their own XML files
    primarykey
    data
    text
    <p>I have an XML file (on the left) and I want to create multiple files (on the right):</p> <pre><code>&lt;ParentNode&gt; file1: &lt;ChildNode&gt; &lt;ParentNode&gt; &lt;node&gt;&lt;/node&gt; &lt;ChildNode&gt; &lt;/childNode&gt; &lt;node&gt;&lt;/node&gt; &lt;ChildNode&gt; &lt;/childNode&gt; &lt;node&gt;&lt;/node&gt; &lt;/ParentNode&gt; &lt;/childNode&gt; file2: &lt;ChildNode&gt; &lt;ParentNode&gt; &lt;node&gt;&lt;/node&gt; &lt;ChildNode&gt; &lt;/childNode&gt; &lt;node&gt;&lt;/node&gt; &lt;/ParentNode&gt; &lt;/childNode&gt; &lt;/ParentNode&gt; </code></pre> <p>I am trying to take the first child node from the original XML file and add it to a new one but I keep getting errors around replacing nodes.</p> <p>I want to do something like the following</p> <pre><code> DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = dbfac.newDocumentBuilder(); Document newDocument; Node firstChild = document.getFirstChild(); NodeList childNodes = firstChild.getChildNodes(); Element parentNode; for (int i = 1; i &lt; childNodes.getLength(); i++ ) { newDocument = docBuilder.newDocument(); parentNode = newDocument.createElement("ParentNode"); newDocument.appendChild(parentNode); newDocument.getFirstChild().appendChild(childNodes.item(i)); } </code></pre> <p>but I get an error</p> <pre><code>org.w3c.dom.DOMException: WRONG_DOCUMENT_ERR: A node is used in a different document than the one that created it. </code></pre> <p>any help pointing in the right direction appreciated!</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