I am creating xml nodes with conditions and want to delete a node if it is having no child nodes:
<main>
<tags>
<tag></tag>
<tag>hello</tag>
<tag>hello1</tag>
</tags>
</main>
I want this output before saving the xml:
<main>
<tags>
<tag>hello</tag>
<tag>hello1</tag>
</tags>
</main>
please ignore node names : if I delete it is deleting but end tag is not removed like </tag>
is still present after remove:
$doc = new DOMDocument;
$doc->load($path);
$element = $doc->documentElement;
$elementtodelete = $element->getElementsByTagName('OthersInc')->item(0);
if(! $elementtodelete->hasChildNodes()) {
$oldelement = $elementtodelete->parentNode->removeChild($elementtodelete);
}