I'm trying to delete all the NodePrices that do not match a specific NodeName, for this example Place2. Here's a sample of the XML
<DocHeader>
<DocTitle>Node Price Report</DocTitle>
<DocRevision>1</DocRevision>
<DocConfidentiality>
<DocConfClass>PUB</DocConfClass>
</DocConfidentiality>
<CreatedAt>2018-02-03T13:02:01</CreatedAt>
</DocHeader>
<DocBody>
<NodePrices>
<NodeName>Place1</NodeName>
<Contact>Employee1</Contact>
</NodePrices>
<NodePrices>
<NodeName>Place2</NodeName>
<Contact>Employee2</Contact>
</NodePrices>
<NodePrices>
<NodeName>Place3</NodeName>
<Contact>Employee3</Contact>
</NodePrices>
</DocBody>
I found a previously asked question that looks like the answer to my question however the results are not what I expected. When I run the code and echo the results they are what I expect, I see Place2.When I save the results to file Place2 is missing all I have is the DocHeader. What am I doing wrong?
The previous post is How to modify xml file using PHP
Here's my PHP
$dom=new DOMDocument();
$dom->load("Nodes.xml");
$root=$dom->documentElement;
$nodesToDelete=array();
$markers=$root->getElementsByTagName('NodePrices');
// Loop trough childNodes
foreach ($markers as $marker) {
$NodeName=$marker->getElementsByTagName('NodeName')->item(0)->textContent;
if($NodeName=='Place2') {
continue;
}
$nodesToDelete[]=$marker;
}
// You delete the nodes
foreach ($nodesToDelete as $node) {
$node->parentNode->removeChild($node);
}
echo $dom->saveXML();
$dom->save('FilteredNodes.xml');