Jump to content

get childs from the DOMElement class


MMDE

Recommended Posts

Heya

 

I'm a bit confused, there's probably an easy solution to this.

 

http://www.php.net/manual/en/class.domelement.php

The class has the methods: appendChild, hasChildNodes, removeChild and replaceChild.

What I really want to do is to get all it's childs, but of course are there no such method. :(

 

What I'm really doing is going through a HTML page and this one DOMElement, an anchor, only has an img tag in it's "nodeValue".

<a href="someurl.html"><img src="somepicture.gif" alt="some picture" /></a>

How do I check if the DOMElement of the anchor contains this img.

 

:s

 

-

MMDE

Link to comment
https://forums.phpfreaks.com/topic/266820-get-childs-from-the-domelement-class/
Share on other sites

http://www.php.net/manual/en/class.domelement.php

The class has the methods: appendChild, hasChildNodes, removeChild and replaceChild.

What I really want to do is to get all it's childs, but of course are there no such method. :(

Because it's not a method.

foreach($node->childNodes as $child) {

http://www.php.net/manual/en/class.domelement.php

The class has the methods: appendChild, hasChildNodes, removeChild and replaceChild.

What I really want to do is to get all it's childs, but of course are there no such method. :(

Because it's not a method.

foreach($node->childNodes as $child) {

 

I didn't notice it extended DOMNode. lol

http://www.php.net/manual/en/class.domnode.php

 

Which has "public readonly DOMNodeList $childNodes ;".

I already tried looping through them as you suggested, but I got some strange error, but I guess I messed something up. o.O

I tried doing this:

$child->getAttribute('alt');

But I got this error:

Call to undefined method DOMText::getAttribute()

So the DOMNodeList it returns is a list of DOMText objects urgh.

http://www.php.net/manual/en/class.domtext.php

Which extends on DOMCharacterData

http://www.php.net/manual/en/class.domcharacterdata.php

Which in turn extends on DOMNode

http://www.php.net/manual/en/class.domnode.php

 

Urgh...

 

At least DOMNode has:

public readonly DOMNamedNodeMap $attributes ;

http://www.php.net/manual/en/class.domnamednodemap.php

 

How do I use this class?

 

I tried doing this:

		foreach($element->childNodes AS $child){
		echo $child->attributes->getNamedItem('alt')."\n";
	}

But got the error:

Call to a member function getNamedItem() on a non-object

 

EDIT:

I tried doing this instead:

		foreach($element->childNodes AS $child){
		print_r($child->attributes);
		echo "\n";
	}

And got no error, but it seems sometimes $child->attributes returns no object, so I guess I just gotta check if it does. :P

To check that I should probably use:

public bool hasAttributes ( void )

 

URGH...

$child->attributes->getNamedItem('alt')

returns a DOMAttr object... -_-'

http://www.php.net/manual/en/class.domattr.php

I guess I'm getting closer now!! :D

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.