Jump to content


Photo

PHP DOM Trouble - Selectively Deleting Nodes


  • This topic is locked This topic is locked
No replies to this topic

#1 mmcdermo

mmcdermo
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 15 October 2006 - 11:40 PM

****
EDIT: Argh! I didn't intend to post this thread in the regex forum, and now I can't seem to find a delete button. I appologize for any inconvenience this causes anyone.. Please move or delete this topic.
****


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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users