Every node in a DOM Document has a list of references to all nodes contained underneath it. These are referred to as its child nodes. In DOMIT!, the child nodes list exists as an array of nodes named childNodes.
To grab a reference to the childNodes array of a node, use the following syntax:
$myChildNodes =& $cdCollection->documentElement->childNodes;
Always remember to use the reference (&) operator, or you will be returned a shallow copy of the childNodes array.
Prior to grabbing a reference to the childNodes array, you can also check if there are indeed any child nodes to be had by using the hasChildNodes() method:
if ($cdCollection->documentElement->hasChildNodes()) {
$myChildNodes =& $cdCollection->documentElement->childNodes;
}
Because the childNodes field is a standard PHP array, you are now able to use the PHP array functions to traverse the childNodes array and access its individuals nodes:
if ($cdCollection->documentElement->hasChildNodes()) {
$myChildNodes =& $cdCollection->documentElement->childNodes;
for ($i = 0; $i < count($myChildNodes); $i++) {
echo ("The node name of child node $i is: " .
$myChildNodes[$i]->nodeName . "\n");
}
}
The above example will return:
The node name of child node 0 is: cd
The node name of child node 1 is: cd
The node name of child node 2 is: cd
|