DOMNode to DOMElement in php

后端 未结 5 1458
南旧
南旧 2021-02-13 20:15

I want to convert a DOMNode object from a call to getElementsByTagName() to a DOMElement in order to access methods like getElements

5条回答
  •  遥遥无期
    2021-02-13 21:13

    You don't need to do any explicit typecasting, just check if your DOMNode object has a nodeType of XML_ELEMENT_NODE.

    PHP will be perfectly happy with this.

    If you use PHPLint to check your code you will notice that PHPLint complains about using getElementsByTagName on a DOMNode object. To get around this you need to jump through the following hoop:

    /*.object.*/ $obj = $node;
    $element = /*.(DOMElement).*/ $obj;
    

    Then you will have a $element variable of the correct type and no complaints from PHPLint.

提交回复
热议问题