I'm trying to figure out how to delete a bunch of nodes if they are empty. I have tried this but with no luck:
$xml = new DOMDocument();
$xml->preserveWhiteSpace = false;
$xml->formatOutput = true;
$xml->loadXML('<library>
<invites>
<invite>
<username>0</username>
<userid>0</userid>
</invite>
</invites>
<invites/>
<invites/>
<invites/>
<invites/>
<invites/>
<invites/>
</library>');
echo "<xmp>OLD \n". $xml->saveXML() ."</xmp>";
$opNodes = $xml->getElementsByTagName('invites');
foreach($opNodes as $node) {
$innerHtml = trim($node->nodeValue);
if(empty($innerHtml)){
$node->parentNode->removeChild($node);
}
}
echo "<xmp>NEW \n". $xml->saveXML() ."</xmp>";
This only removes some of the , Why?... Please help and thanks in advance :-)