0

Possible Duplicate:
DOMElement cloning and appending: ‘Wrong Document Error’

I would like to copy all the child nodes of element foo in DOMDocument A into element bar in DOMDocument B. However, using appendChild to do so apparently throws a DOM_WRONG_DOCUMENT_ERR.

Is there a right way of doing this?

XML Document A

<foo>
    <child />
    <child />
    <child />
</foo>

XML Document B

<bar>
    <other-child />
    <other-child />
</bar>

Resulting DOMDocument:

<bar>
    <other-child />
    <other-child />
    <child />
    <child />
    <child />
</bar>

The elements should not be assumed to be empty, but are arbitrarily complex.

Community
  • 1
  • 1
Eric
  • 95,302
  • 53
  • 242
  • 374
  • Can you provide some code example? – antyrat Jul 20 '10 at 16:43
  • 1
    On a side note: `DomDocument` & `DOMDocument` are entirely different things in PHP jargon. `DomDocument` was a pre-php5 dom xml implementation: http://nl2.php.net/manual/en/book.domxml.php Although php4 should be dead, keeping case & thereby version clear when talking about it is advisable. – Wrikken Jul 20 '10 at 16:49

1 Answers1

3

You will probably want to take a look at DOMDocument::importNode.

This function returns a copy of the node to import and associates it with the current document.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088