Jump to content


Photo

PHP DOM Trouble - Selectively Deleting Nodes


  • Please log in to reply
2 replies to this topic

#1 mmcdermo

mmcdermo
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 15 October 2006 - 11:48 PM

****
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] :

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 = &lt;"; echo $parent_node->nodeName; echo "&gt;<br/>";
                        $child_to_delete = $node_list->item($i_position);
                        echo "Child Node = &lt;"; echo $child_to_delete->nodeName; echo "&gt;<br/>";
                        if($child_to_delete->$cNode->textContent == $textContentValue){
                          $parent_node->removeChild($child_to_delete);
                          $i_position--;
                        }
                        $i_position++;
                }
    }

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:

$parent_node->removeChild($child_to_delete);

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,
~Morgan McDermott.


UPDATE: [I forgot to post the output, before.]
From the echo commands I built into the function, my output is this:

Run Through 0:0
Parent Node = <tier2_obj>
Child Node = <tier3_obj>

--------------------------------------------------------------------------------

Run Through 1:0
Parent 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.

#2 marcus

marcus
  • Members
  • PipPipPip
  • Advanced Member
  • 1,842 posts
  • LocationRochester, NY

Posted 15 October 2006 - 11:51 PM

just lock it

#3 mmcdermo

mmcdermo
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 16 October 2006 - 12:00 AM

Done. Thank you  :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users