I have extracted a DomNode from a XML. I then tried to insert it to another DomNode located in a different DOMDocument using appendChild(DOMNode*) but I got a DOMException.
The exception:
node is used in a different document than the one that created it
The question:
How can I move a DomNode from one DOMDocument to another?
I go ahead an answer this question, The person who asked the question led me to this answer but it took some time for me to figure out the whole concept.
// Result is from an xpath query
while(result->iterateNext())
{
// Creating the new document
DOMDocument * doc = this->domImplementation->createDocument();
// Importing the node from the old document to the new document scope
DOMNode * node = doc->importNode(result->getNodeValue(), true);
// Appending the node to the new document
doc->appendChild(node);
...
As seen above, first you need to import the node to the document to give the node an owner document, then append it to that document where ever you want it to be placed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With