Since it is a tree-like structure, each node in a DOM Document has a list of references to the 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 any child nodes exist, by using the hasChildNodes() method:
if ($cdCollection->documentElement->hasChildNodes()) {
$myChildNodes =& $cdCollection->documentElement->childNodes;
}
The number of child nodes is stored in the childCount property.
You can use this value to traverse the childNodes array and access its individuals nodes:
if ($cdCollection->documentElement->hasChildNodes()) {
$myChildNodes =& $cdCollection->documentElement->childNodes;
$numChildren =& $cdCollection->documentElement->childCount;
for ($i = 0; $i < $numChildren; $i++) {
echo ("The node name of child node $i is: " .
$myChildNodes[$i]->nodeName . "<br />");
}
}
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
|