Alright, apologies in advance, but this post is going to be rather long-winded.
First of all, in order to get the function you gave me to run properly, I had to remove '$cNode' from everything, as it didn't want to function properly. I believe that this was part of my problem... I'm not sure if the PHP DOM objects allow you to access eachother like
$node1->node2->textContent
Or at least, that seems to be the problem here. When I left in '$cNode->', the textContent property of the object [ $child_to_delete->$cNode->textContent ] would always be blank.
The solution to get your script to run, then, was to remove $cNode and change the functionality slightly. I changed it to delete any node whose textContent matched the third function argument [ $textContentValue ].
Modified function:
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/>";
echo 'textContent of $child_to_delete:' . $child_to_delete->textContent . '<br>'; //$cNode->
echo '$textContentValue:' . $textContentValue . '<br>';
echo 'comparison result:' . ($child_to_delete->textContent == $textContentValue) . '<br>'; // $cNode->
if($child_to_delete->textContent == $textContentValue){ //->$cNode
echo ('DELETING<br>');
$parent_node->removeChild($child_to_delete);
$i_position--;
}
$i_position++;
} // for i...
print_r($node_list);
$file = "$this->source"."1";
$xml_file->save($file);
}
The output from this function when run with arguments "tier3_obj", '', 'Some Text'
is :
---------------Output-------------
Run Through 0:0
Parent Node = <tier2_obj>
Child Node = <tier3_obj>
textContent of $child_to_delete:Some Text
$textContentValue:Some Text
comparison result:1
DELETING
Run Through 1:0
Parent Node = <tier2_obj>
Child Node = <tier3_obj>
textContent of $child_to_delete😮ther Text
$textContentValue:Some Text
comparison result:
Run Through 2:1
Parent Node = <tier2_obj>
Child Node = <tier3_obj>
textContent of $child_to_delete😮ther Text
$textContentValue:Some Text
comparison result:
Run Through 3:2
Parent Node = <tier2_obj>
Child Node = <tier3_obj>
textContent of $child_to_delete😮ther Text
$textContentValue:Some Text
comparison result:
Run Through 4:3
Parent Node = <tier2_obj>
Child Node = <tier3_obj>
textContent of $child_to_delete:Some Text
$textContentValue:Some Text
comparison result:1
DELETING
Run Through 5:3
Parent Node = <tier2_obj>
Child Node = <tier3_obj>
textContent of $child_to_delete😮ther Text
$textContentValue:Some Text
comparison result:
DOMNodeList Object ( )
--------------------/Output-----------------------
In the file, the contents were as follows:
<?xml version="1.0" encoding="ISO-8859-1"?>
<tier1_obj>
<tier2_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>Other Text</tier3_obj>
</tier2_obj>
</tier1_obj>
So, removing the extra node in the (reference? What do you call this 😃 ?) made it functional... Now, I'm wondering if I could have the function remove a node if a certain attribute [like an ID attribute] were equal to one of the function's arguments. I'm also very, very curious as to why accessing a node property like $node1->node2->property doesn't work.
Thanks a lot for your help sneakyimp. I have a fair idea of what my problem was, although all this PHP DOM stuff is still swirling around in my head as I try to get a firm grasp on it. Anyways, if anyone would be so kind as to help me figure out why I couldn't access node properties like that, and if accessing attributes WOULD work instead, I'd be greatly appreciative.
:evilgrin: Thanks again, sneaky. I feel as if I have a firmer grip on what I must do, now.