• Advertisement
Sign in to follow this  

[web] PHP - Deleting Childs

This topic is 2622 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
Advertisement
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]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement