[web] PHP - Deleting Childs

Started by
2 comments, last by Anntor 13 years, 5 months ago
I have an xml file, for some reason I can't figure out how to delete the childs.
the file:
<user>
<friend>bob</friend>
<friend>joe<friend>
</user>

I'm trying to delete joe. I want the entire: "<friend>joe</friend>" to be deleted form the file, I can't seem to do it! HELP!
Advertisement
perhaps XMLStarlet can help you.

This is not essentially a PHP issue.
Are you going to be updating this XML from your PHP? Perhaps the data would be better stored in a database?
As of PHP 5.0, SimpleXML is part of PHP. It requires the libxml PHP extension.

<?php$xml = simplexml_load_file('test.xml');// write new xml to buffer$buffer = "<user>\n";foreach($xml->children() as $child) { if ("$child" !== "joe") $buffer .= "<friend>$child</friend>\n";}$buffer .= "<user>\n";// write buffer to file$file = fopen('test.xml', 'w');fwrite($file, $buffer);fclose($file);?>


[Edited by - Anntor on November 15, 2010 12:06:18 PM]

This topic is closed to new replies.

Advertisement