MMDE Posted August 8, 2012 Share Posted August 8, 2012 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 Quote Link to comment Share on other sites More sharing options...
requinix Posted August 8, 2012 Share Posted August 8, 2012 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) { Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 8, 2012 Share Posted August 8, 2012 BTW the plural of child is children Quote Link to comment Share on other sites More sharing options...
MMDE Posted August 8, 2012 Author Share Posted August 8, 2012 BTW the plural of child is children I realized that after I posted it. Posted it in a hurry. Quote Link to comment Share on other sites More sharing options...
MMDE Posted August 8, 2012 Author Share Posted August 8, 2012 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. 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!! Quote Link to comment Share on other sites More sharing options...
MMDE Posted August 8, 2012 Author Share Posted August 8, 2012 Solved it: $elements = $domdocument->getElementsByTagName('a'); foreach($elements AS $element){ foreach($element->childNodes AS $child){ if($child->hasAttributes()){ echo $child->attributes->getNamedItem('alt')->value."\n"; } } } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.