note 84118 modified in function.simplexml-element-addChild by danbrown

From: Date: Wed, 16 Sep 2009 16:38:32 +0000
Subject: note 84118 modified in function.simplexml-element-addChild by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-160741@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. 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

« previous php.notes (#160741) next »