DOMNode::hasChildNodes

(PHP 5)

DOMNode::hasChildNodes Checks if node has children

Description

public bool DOMNode::hasChildNodes ( void )

This function checks if the node has children.

Return Values

Returns TRUE on success or FALSE on failure.

See Also

Коментарии

<?php

$doc 
= new DOMDocument;
$node $doc->createElement("FirstMain""First Main Node. This have child");
$doc->appendChild($node);
$childnode $doc->createElement("childnode""child node");
$node->appendChild($childnode);
$secondnode $doc->createElement("SecondMain""First Main Node. This don't have child");
$doc->appendChild($secondnode);
$doc->saveXML();
$nodeElmt $doc->getElementsByTagName("FirstMain");
/*
if given as $nodeElmt = $doc->getElementsByTagName("childnode");
the output would be "This node has childnodes"

if given as $nodeElmt = $doc->getElementsByTagName("SecondMain");
the output would be "This node has no childnodes"

*/

foreach($nodeElmt as $nodeElmt)
{
if(
$nodeElmt->hasChildNodes())
{
echo 
"This node has childnodes";
}
else
{
echo 
"This node has no childnodes";
}
}

 
?>

- - - - - - - - - - - - - -

Output:

This node has childnodes

- - - - - - - - - - - - - -
2009-04-28 07:13:59
http://php5.kiev.ua/manual/ru/domnode.haschildnodes.html
This function is a bit tricky. If you want to find XML childnodes it is useless. You need to create a work-around:

<?php

$x 
= new DOMDocument();
$x->loadXML('
<A>
 <B>b-text</B>
 <C>
  <D>d-text</D>
 </C>
 <E/>
</A>'
);

shownode($x->getElementsByTagName('A')->item(0));
function 
shownode($x) {
 foreach (
$x->childNodes as $p)
  if (
hasChild($p)) {
      echo 
$p->nodeName.' -> CHILDNODES<br>';
     
shownode($p);
  } elseif (
$p->nodeType == XML_ELEMENT_NODE)
   echo 
$p->nodeName.' '.$p->nodeValue.'<br>';
}
function 
hasChild($p) {
 if (
$p->hasChildNodes()) {
  foreach (
$p->childNodes as $c) {
   if (
$c->nodeType == XML_ELEMENT_NODE)
    return 
true;
  }
 }
 return 
false;
}

?>

shows:
B b-text
C -> CHILDNODES
D d-text
E
2010-01-04 07:53:44
http://php5.kiev.ua/manual/ru/domnode.haschildnodes.html
Автор:
Personally I think using a simple:[code]if($DOMNode->childNodes <>0){}[/code] works better.
2010-10-14 22:04:57
http://php5.kiev.ua/manual/ru/domnode.haschildnodes.html
This "hasChildNodes()" exercise is simple enough to make it clear and understandable. Or, you could take it as a tag empty check. By Richard Holm, Sweden.

<?php
$xmldoc
=
'<?xml version="1.0" ?>
<root>
<text>Text</text>
<none/>
<empty></empty>
<space> </space>
</root>'
;

$domdoc=new DOMDocument();
$domdoc->loadXML($xmldoc);

$tag=$domdoc->getElementsByTagName('root')->item(0);
$v=$tag->hasChildNodes()?" hasChildNodes":" hasNoChildNodes";
echo 
$tag->tagName.$v."<br/>";

$tag=$domdoc->getElementsByTagName('text')->item(0);
$v=$tag->hasChildNodes()?" hasChildNodes":" hasNoChildNodes";
echo 
$tag->tagName.$v."<br/>";

$tag=$domdoc->getElementsByTagName('none')->item(0);
$v=$tag->hasChildNodes()?" hasChildNodes":" hasNoChildNodes";
echo 
$tag->tagName.$v."<br/>";

$tag=$domdoc->getElementsByTagName('empty')->item(0);
$v=$tag->hasChildNodes()?" hasChildNodes":" hasNoChildNodes";
echo 
$tag->tagName.$v."<br/>";

$tag=$domdoc->getElementsByTagName('space')->item(0);
$v=$tag->hasChildNodes()?" hasChildNodes":" hasNoChildNodes";
echo 
$tag->tagName.$v."<br/>";
?>

Output:
root hasChildNodes
text hasChildNodes
none hasNoChildNodes
empty hasNoChildNodes
space hasChildNodes
2012-07-29 04:20:23
http://php5.kiev.ua/manual/ru/domnode.haschildnodes.html
Автор:
Hi what if its a dynamic file and we cannot use get elements by tag name then how do we print the contents of all level tags?
2015-03-21 18:33:43
http://php5.kiev.ua/manual/ru/domnode.haschildnodes.html

    Поддержать сайт на родительском проекте КГБ