note 48814 deleted from function.simplexml-load-string by cmb

From: Date: Sun, 15 Dec 2019 10:08:55 +0000
Subject: note 48814 deleted from function.simplexml-load-string by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-213523@lists.php.net to get a copy of this message
Note Submitter: igor kraus ---- A simple way to merge two SimpleXML objects. <?php /** * Pumps all child elements of second SimpleXML object into first one. * * @param object $xml1 SimpleXML object * @param object $xml2 SimpleXML object * @return void */ function simplexml_merge (SimpleXMLElement &$xml1, SimpleXMLElement $xml2) { // convert SimpleXML objects into DOM ones $dom1 = new DomDocument(); $dom2 = new DomDocument(); $dom1->loadXML($xml1->asXML()); $dom2->loadXML($xml2->asXML()); // pull all child elements of second XML $xpath = new domXPath($dom2); $xpathQuery = $xpath->query('/*/*'); for ($i = 0; $i < $xpathQuery->length; $i++) { // and pump them into first one $dom1->documentElement->appendChild( $dom1->importNode($xpathQuery->item($i), true)); } $xml1 = simplexml_import_dom($dom1); } $xml1 = simplexml_load_string('<root><child>child 1</child></root>'); $xml2 = simplexml_load_string('<root><child>child 2</child></root>'); simplexml_merge($xml1, $xml2); echo($xml1->asXml()); ?> Will output: <?xml version="1.0"?> <root> <child>child 1</child> <child>child 2</child> </root>

« previous php.notes (#213523) next »