Sign in to follow this  
danlthemanl

[web] PHP - Deleting Childs

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
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this