<?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
- - - - - - - - - - - - - -
DOMNode::hasChildNodes
(PHP 5)
DOMNode::hasChildNodes — Checks if node has children
Description
bool DOMNode::hasChildNodes
( void
)
This function checks if the node has children.
Return Values
Returns TRUE on success or FALSE on failure.
DOMNode::hasChildNodes
jintoppy at gmail dot com
28-Apr-2009 06:13
28-Apr-2009 06:13
