rblog

WRONG_DOCUMENT_ERR: A node is used in a different document than the one that created it

Spent hours struggling with the stupid error “WRONG_DOCUMENT_ERR: A node is used in a different document than the one that created it”. I have two XML-documents one, which I need to copy a given node from and the other file where I first needed to remove the matching node then copy it from source to the target file. Trick is to use importNode, not just cloneNode


def copyOfSource = sourceProviders[0].cloneNode(true)
targetRoot.appendChild(targetDoc.importNode(copyOfSource, true))

Hours of work solved in few minutes just refining my Google search. Was about to post a question on http://stackoverflow.com and while preparing the question I needed some more background information. Tada…