Jump to content
  • Advertisement
Sign in to follow this  
danlthemanl

[web] PHP - Deleting Childs

This topic is 2804 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
Are you going to be updating this XML from your PHP? Perhaps the data would be better stored in a database?

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!