mmcdermo Posted October 15, 2006 Share Posted October 15, 2006 ****EDIT: If an admin/mod is reading this, please delete my post that is identical to this found in the regex forum. I accidentlaly made the post there, and I don't see any delete button to undo my mistake. Thank you! ;)**** My goal here is to create a function that deletes nodes from an XML file if it meets certain criterion passed to the function.Here's the basic structure of the XML file I'm trying to delete nodes from:<?xml version="1.0" encoding="ISO-8859-1"?><tier1_obj><tier2_obj><tier3_obj>Some Text</tier3_obj><tier3_obj>Other Text</tier3_obj></tier2_obj><tier2_obj><tier3_obj>Other Text</tier3_obj><tier3_obj>Other Text</tier3_obj></tier2_obj><tier2_obj><tier3_obj>Some Text</tier3_obj><tier3_obj>Other Text</tier3_obj></tier2_obj></tier1_obj>So, let's say that we want to delete any tier2_obj's that have a tier3_obj thats textContent property is "Some Text". Here's the function I came up with to do this [Note the $i_position variable. I use this because once you delete a node from $node_list, the other nodes' indicies are reduced by 1. Every time I delete a node, I subtract one from $i_position] :[code]function selectivelyDeleteNodes ($tagname, $cNode, $textContentValue) { // Find all elements with specefied $tagname $xml_file = new DOMDocument; $xml_file->load($some_xml_file); $xml_file_delm = $xml_file->documentElement; $node_list= $xml_file_delm->getElementsByTagName("$tagname"); $num_nodes = $node_list->length; //Number of matching nodes for ($i = 0; $i < $num_nodes; $i++ ) { if(!isset($i_position)){$i_position = 0;} //For first run, initiate var i_position with value 0 echo "<hr/><br/>Run Through $i:$i_position<br/>"; $parent_node = $node_list->item($i_position)->parentNode; echo "Parent Node = <"; echo $parent_node->nodeName; echo "><br/>"; $child_to_delete = $node_list->item($i_position); echo "Child Node = <"; echo $child_to_delete->nodeName; echo "><br/>"; if($child_to_delete->$cNode->textContent == $textContentValue){ $parent_node->removeChild($child_to_delete); $i_position--; } $i_position++; } }[/code]Whenever I run this function, it never deletes the <tier2_obj> even when I modify the if statement to something like if(1=1).. So I believe my problem comes down to the line:[code]$parent_node->removeChild($child_to_delete);[/code]Although it's really quite hard for me to tell.I'd appreciate any help on the subject, if even that help is just a link to somewhere that I might get the inspiration to figure out what's wrong with my code here .Thank you for your valuable time,[i]~Morgan McDermott.[/i][b]UPDATE: [I forgot to post the output, before.][/b]From the echo commands I built into the function, my output is this:Run Through 0:0Parent Node = <tier2_obj>Child Node = <tier3_obj>--------------------------------------------------------------------------------Run Through 1:0Parent Node = <tier2_obj>Child Node = <tier3_obj>What I get from this output is that the script is selecting the proper child nodes, and is running through the proper number [$node_list->length] of times. However, when I look at the XML output in a file [ In testing, I added $xml_file->save($file);], the nodes are still very much there. Quote Link to comment Share on other sites More sharing options...
marcus Posted October 15, 2006 Share Posted October 15, 2006 just lock it Quote Link to comment Share on other sites More sharing options...
mmcdermo Posted October 16, 2006 Author Share Posted October 16, 2006 Done. Thank you :) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.