1

How can I combine two or more simpleXML objects?

$xml1 = simplexml_load_file($file1);
$xml2 = simplexml_load_file($file2);
...
Anne M
  • 11
  • 1
  • 2

2 Answers2

1

You can typecast the XML into an array, merge ( http://php.net/manual/en/function.array-merge.php) it, then typecast back to an object. Like so:

$xml = (object)array_merge((array)$xml1, (array)$xml2);
casraf
  • 21,085
  • 9
  • 56
  • 91
-1

You can create a new class and add those objects to the class to get a combined object.

class myXML {
  function add($xmlobj) {
    foreach ($xmlobj->children() as $key=>$obj) {
        $this->$key = $obj;
    }
  }
}

$xob1 = simplexml_load_file($file1);
$xob2 = simplexml_load_file($file2);
$xob3 = new myXML();
$xob3->add($xob1);
$xob3->add($xob2);
Ray Perea
  • 5,640
  • 1
  • 35
  • 38