Forum Moderators: coopster
My structure i:
<values>
<user>
<userid>21</userid>
</user>
<user>
<userid>48</userid>
</user>
</values>
I have read quite a bit on this, and I believe this cannot be done with SimpleXML, and needs to be DOM. I am looking for a simple solution to this. Does anyone know how to delete the entire <user> tag if the <userid> of that tag is 48?
Thanks in advance. Hope I am clear enough.
$string = <<<ENDOFHTML
<values>
<user>
<userid>21</userid>
</user>
<user>
<userid>48</userid>
</user>
</values>
ENDOFHTML;
$xml = simplexml_load_string($string);
$count = 0;
foreach ($xml as $user) {
if ($user->userid == 21) {
unset($xml->user[$count]); break;
}
$count++;
}
echo $xml->asXML();
exit;
$xmlfile = ".. .xml"; (location of xml file)
$xmlstr = file_get_contents("$xmlfile");
$xml = new SimpleXMLElement($xmlstr);
$count = 0;
foreach ($xml as $user){
if ($user->userid == "02345678"){
unset($xml->user[$count]); break;
}
$count++;
}
echo $xml->asXML();
$handle = fopen("$xmlfile", "w");
fwrite($handle, $xml->asXML());
fclose($handle);
exit;
?>