note 84118 modified in function.simplexml-element-addChild by thiago
| From: | thiago@php.net | Date: | Wed, 20 Aug 2008 02:01:18 +0000 |
| Subject: | note 84118 modified in function.simplexml-element-addChild by thiago | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-143647@lists.php.net to get a copy of this message | ||
Here's a handy workaround to accommodate the lack of a $xml->deleteChild() function.
It's not perfect, but it's a start (and it works for what I'm using it for):
<?php
// Workaround by Kobra.
function simplexml_deleteChild($parent, $childname, $parentname)
{
$temp = simplexml_load_string(str_replace("#","?","<#xml
version=\"1.0\"#>\n<".$parentname.">\n</".$parentname.">"));
foreach($parent as $t)
{
if($t->getName() != $childname)
{
$name = $t->getName();
$value = eval("return \$parent->".$name.";");
$temp->addChild($t->getName(), $value);
}
}
return $temp;
}
?>
I have not tested this with multi-level XML files; but like I said, it's a start.
--was--
Here's a handy workaround to accommodate the lack of a $xml->deleteChild() function.
It's not perfect, but it's a start (and it works for what I'm using it for):
<?php
// Workaround by Kobra. http://www.kobrasrealm.net
function simplexml_deleteChild($parent, $childname, $parentname)
{
$temp = simplexml_load_string(str_replace("#","?","<#xml
version=\"1.0\"#>\n<".$parentname.">\n</".$parentname.">"));
foreach($parent as $t)
{
if($t->getName() != $childname)
{
$name = $t->getName();
$value = eval("return \$parent->".$name.";");
$temp->addChild($t->getName(), $value);
}
}
return $temp;
}
?>
I have not tested this with multi-level XML files; but like I said, it's a start.
http://php.net/manual/en/function.simplexml-element-addChild.php