DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Haydn has posted 6 posts at DZone. View Full User Profile

Functions To Add / Remove Nodes To / From An XML File Using PHP.

08.15.2010
| 6204 views |
  • submit to reddit
        // description of your code here

<?php
function removNode($myXML, $node, $attribute, $id) {
    $xmlDoc = new DOMDocument();
    $xmlDoc->load($myXML);
    $xpath = new DOMXpath($xmlDoc);
   
    if( $attribute!='' || $id!='' )
    $nodeList = $xpath->query('//'.$node.'[@'.$attribute.'="'.$id.'"]');
    else
    $nodeList = $xpath->query('//'.$node.'');
   
    if ($nodeList->length)
    {
        $node = $nodeList->item(0)  ;
        $node->parentNode->removeChild($node);
    }
    $xmlDoc->save($myXML) ;
}   
?>
<a href="http://www.freemanagementgames.com"> Management Games </a>