I am currently writing a PHP class which handles an XML document. Specifically, a certain method of such class requires to know whether a node has children or whether it is a "leaf". Suppose an XML structure as follows:
<root>
<obj1>
<obj2>red</obj1>
<obj3>green</obj3>
<obj4>
<obj5></obj5>
</obj4>
</obj1>
</root>
I would consider root, obj1 and obj4 to be nodes, while obj2, obj3 and obj5 to be leaves, not having child nodes themselves.
The code I use is the following:
$MyDoc = NewDOMDocument();
$node = $MyDoc->getElementsByTagName($node_name);
foreach($node as $node) {
if($node->hasChildNodes()) {
return true;
}
return false;
}
However, such code is returning "true" for any object I apply it to, whether it has childNodes or not.
What am I doing wrong?