Merge two XML files recursively
This is nice solution from comment on PHP manual page, working also with attributes too:
function append_simplexml(&$simplexml_to, &$simplexml_from){ foreach ($simplexml_from->children() as $simplexml_child) { $simplexml_temp = $simplexml_to->addChild($simplexml_child->getName(), (string) $simplexml_child); foreach ($simplexml_child->attributes() as $attr_key => $attr_value) { $simplexml_temp->addAttribute($attr_key, $attr_value); } append_simplexml($simplexml_temp, $simplexml_child); }}
There also is sample of usage.