note 91561 added to ref.simplexml
| From: | info at ajaxforum dot ru | Date: | Wed, 17 Jun 2009 07:32:40 +0000 |
| Subject: | note 91561 added to ref.simplexml | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-156953@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);
}
}
----
Server IP: 69.147.83.197
Probable Submitter: 193.111.92.112
----
Manual Page -- http://www.php.net/manual/en/ref.simplexml.php
Edit -- https://master.php.net/note/edit/91561
Del: integrated -- https://master.php.net/note/delete/91561/integrated
Del: useless -- https://master.php.net/note/delete/91561/useless
Del: bad code -- https://master.php.net/note/delete/91561/bad+code
Del: spam -- https://master.php.net/note/delete/91561/spam
Del: non-english -- https://master.php.net/note/delete/91561/non-english
Del: in docs -- https://master.php.net/note/delete/91561/in+docs
Del: other reasons-- https://master.php.net/note/delete/91561
Reject -- https://master.php.net/note/reject/91561
Search -- https://master.php.net/manage/user-notes.php