Jump to content

PHP DOM Trouble - Selectively Deleting Nodes


mmcdermo

Recommended Posts

****
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 = &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++;
                }
    }
[/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: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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.