DOMNode to DOMElement in php

You don't need to cast anything, just call the method:

$links = $dom->getElementsByTagName('a');

foreach ($links as $link) {
    $spans = $link->getElementsByTagName('span');
}

And by the way, DOMElement is a subclass of DOMNode. If you were talking about a DOMNodeList, then accessing the elements in such a list can be done, be either the method presented above, with a foreach() loop, either by using the item() method of DOMNodeList:

$link_0 = $dom->getElementsByTagName('a')->item(0);

This is what I use in my project to minimize IDE warning.

/**
 * Cast a DOMNode into a DOMElement
 */
    function cast_e(DOMNode $node) : DOMElement {
    if ($node) {
        if ($node->nodeType === XML_ELEMENT_NODE) {
            return $node;
        }
    }
    return null;
}

Tags:

Php

Xml

Dom