note 91561 modified in ref.simplexml by danbrown

From: Date: Wed, 17 Jun 2009 12:12:02 +0000
Subject: note 91561 modified in ref.simplexml by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-156969@lists.php.net to get a copy of this message
Append/combine/merge one simplexml to another: <?php include 'example.php'; $el1 = new SimpleXMLElement($xmlstr); $el2 = new SimpleXMLElement($xmlstr); // wrong way! // php convert $el2->movie[0] to string silently $el1->addChild('movie', $el2->movie[0]); // wrong way! // php 5.1 convert $el2->movie[0] to string and generate Warning: It is not yet possible to assign complex types to properties. It is not possible to assign complex types to nodes // php 5.2 convert $el2->movie[0] to string silently $el1->addChild('movie'); $el1->movie[2] = $el2->movie[0]; // right way! $el1_movie = $el1->addChild('movie'); append_simplexml($el1_movie, $el2->movie[0]); echo "<pre>"; print_r($el1); echo "</pre>"; /** * Add one simplexml to another * * @param object $simplexml_to * @param object $simplexml_from * @author Boris Korobkov * @link http://www.ajaxforum.ru/ */ 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); } } ?> --was-- Append/combine/merge one simplexml to another: <?php include 'example.php'; $el1 = new SimpleXMLElement($xmlstr); $el2 = new SimpleXMLElement($xmlstr); // wrong way! // php convert $el2->movie[0] to string silently $el1->addChild('movie', $el2->movie[0]); // wrong way! // php 5.1 convert $el2->movie[0] to string and generate Warning: It is not yet possible to assign complex types to properties. It is not possible to assign complex types to nodes // php 5.2 convert $el2->movie[0] to string silently $el1->addChild('movie'); $el1->movie[2] = $el2->movie[0]; // right way! $el1_movie = $el1->addChild('movie'); append_simplexml($el1_movie, $el2->movie[0]); echo "<pre>"; print_r($el1); echo "</pre>"; /** * Add one simplexml to another * * @param object $simplexml_to * @param object $simplexml_from * @author Boris Korobkov * @link http://www.ajaxforum.ru/ */ 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); } } http://php.net/manual/en/ref.simplexml.php

« previous php.notes (#156969) next »