Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception "node is used in a different document than the one that created it" using C++ xerces library

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?

like image 424
user1808932 Avatar asked Jan 18 '26 21:01

user1808932


1 Answers

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.

like image 166
superhero Avatar answered Jan 20 '26 11:01

superhero