Merge two XML files recursively Merge two XML files recursively xml xml

Merge two XML files recursively


xml2array is a function that converts an xml document to an array. Once the two arrays are created, you can use array_merge_recursive to merge them. Then you can convert the array back to xml with XmlWriter (should already be installed).


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.