DOMNode到DOMElement在php [英] DOMNode to DOMElement in php
问题描述
我想将DOMNode对象从调用转换为getElementsByTagName到DOMElement,以便访问子元素上的getElementsByTagName等方法。以任何其他语言,我会投入,这将很容易,但经过一些快速查看,PHP没有对象投射。所以我需要知道的是如何从DOMNode对象获取一个DOMElement对象。
I want to convert a DOMNode object from a call to getElementsByTagName to a DOMElement in order to access methods like getElementsByTagName on the child element. In any other language, I would cast and it would be easy, but after some quick looking, PHP does not have object casting. So what I need to know is how to get a DOMElement object from a DOMNode object.
谢谢。
推荐答案
您不需要投掷任何东西,只需调用方法:
You don't need to cast anything, just call the method:
$links = $dom->getElementsByTagName('a');
foreach ($links as $link) {
$spans = $link->getElementsByTagName('span');
}
顺便说一句,DOMElement是DOMNode的一个子类。如果你正在谈论一个DOMNodeList,那么可以通过使用DOMNodeList的item()方法来获取这样一个列表中的元素,或者是上面提到的方法,使用foreach循环,或者通过使用DOMNodeList的item()方法
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);
这篇关于DOMNode到DOMElement在php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!