I have an XML file below and I want to create multiple files:
<ParentNode> file1:
<ChildNode> <ParentNode>
<node></node> <ChildNode>
</childNode> <node></node>
<ChildNode> </childNode>
<node></node> </ParentNode>
</childNode> file2:
<ChildNode> <ParentNode>
<node></node> <ChildNode>
</childNode> <node></node>
</ParentNode> </childNode>
</ParentNode>
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.
I want to do something like the following
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 < childNodes.getLength(); i++ ) {
newDocument = docBuilder.newDocument();
parentNode = newDocument.createElement("ParentNode");
newDocument.appendChild(parentNode);
newDocument.getFirstChild().appendChild(childNodes.item(i));
}
but I get an error
org.w3c.dom.DOMException: WRONG_DOCUMENT_ERR: A node is used in a different document than the one that created it.
any help pointing in the right direction appreciated!