is there a way to retrieve only the immediate children found by a call to DOMElement::getElementsByTagName? For example, I have an XML document that has a category element.
Something like this should do
/**
* Traverse an elements children and collect those nodes that
* have the tagname specified in $tagName. Non-recursive
*
* @param DOMElement $element
* @param string $tagName
* @return array
*/
function getImmediateChildrenByTagName(DOMElement $element, $tagName)
{
$result = array();
foreach($element->childNodes as $child)
{
if($child instanceof DOMElement && $child->tagName == $tagName)
{
$result[] = $child;
}
}
return $result;
}
edit: added instanceof DOMElement check