note 84118 deleted from simplexmlelement.addChild by danbrown
| From: | danbrown@php.net | Date: | Fri, 28 Jan 2011 14:34:32 +0000 |
| Subject: | note 84118 deleted from simplexmlelement.addChild by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-176462@lists.php.net to get a copy of this message | ||
Note Submitter: kobrasrealm at gmail dot com
----
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.