Jak mogę łatwo połączyć dwa dokumenty XML z tym samym węzłem nadrzędnym w jeden dokument?

Zdecydowałem, że nie ma sposobu, aby to zrobić za pomocą SimpleXMLElements. Czytałem podręcznik PHP DOMDocument i myślę, że mógłbym to zrobić z iteracją, ale to wydaje się nieefektywne. Czy jest lepszy sposób, który mnie nie dotyczy?

Psuedokodowe rozwiązanie iteracyjne:

<code>// two DOMDocuments with same root element
$parent = new ...
$otherParent = new ...

$children = $parent->getElementByTagName('child');
foreach ($children as $child) {
   $otherParent->appendChild($child);
}
</code>

Dla jasności mam dwa dokumenty XML, które wyglądają tak:

<code>    <parent>
      <child>
        <childOfChild>
           {etc, more levels of nested XML trees possible}
        </childOfChild>
      </child>
      <child>
        <childOfChild>
           {etc, more levels possible}
        </childOfChild>
      </child>

</parent>
</code>

Chciałbym, aby wyjście było takie:

<code><parent>
  {all children of both original XML docs, order unimportant, that preserves any nested XML trees the children may have}
<parent>
</code>

questionAnswers(1)

yourAnswerToTheQuestion