Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DOMNode to DOMElement in php

Tags:

dom

php

xml

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.

like image 570
Samuel Avatar asked Jun 15 '09 00:06

Samuel


People also ask

How to create dom in PHP?

$dom = new DOMDocument('1.0', 'utf-8'); $element = $dom->createElement('foo', 'me & you');

How to use dom in PHP?

Dom parser travels based on tree based and before access the data, it will load the data into dom object and it will update the data to the web browser. Below Example shows how to get access to the HTML data in web browser. <? $cols->item(0)->nodeValue.

Can PHP manipulate the DOM?

So if you're ever working with the content for a post (a post type or a custom post type, for that matter) and you need to manipulate tags much like you would with JavaScript, then using the DomDocument library is one of the most powerful tools are your disposal.

What is DOM document in PHP?

The DOMDocument::getElementsByTagName() function is an inbuilt function in PHP which is used to return a new instance of class DOMNodeList which contains all the elements of local tag name.


Video Answer


4 Answers

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);
like image 95
Ionuț G. Stan Avatar answered Oct 01 '22 05:10

Ionuț G. Stan


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;
}
like image 34
Siu Pang Tommy Choi Avatar answered Oct 01 '22 05:10

Siu Pang Tommy Choi


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.

like image 26
Dominic Sayers Avatar answered Sep 30 '22 05:09

Dominic Sayers


I know this is mostly an annoying IDE problem.

The reason is $DOMNodeList->item(0) witch returns a DOMNode ( or at least the IDE thinks so ).

To fix this you will have to Extract out the $DOMDocument->getElementsByTagName( $tagName )->item($index) into a method of its own. In the DOCBlock you set the @return statement to DOMElement witch fixes the inspection problem.

This Works at least in PHPStorm.

like image 25
Theo Jager Avatar answered Sep 28 '22 05:09

Theo Jager