SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2007
    0 Post(s)
    0 Thread(s)

    $dom ->Call to a member function getAttributeNode() on a non-object

    Hi there
    I am running a php script here, trying to catch all the anchors from a page. The problem is that most of the anchors are in <li> tag.
    PHP Code:
    foreach (
    $eTagName as $tagName){
            echo "element <br />";
    $attr=$value->Item(0)->getAttributeNode("href");//attr of a non-object
    "Result 2: {$output1}::::::::::::{$output}<br />";
    Then i get an error
    Call to a member function getAttributeNode() on a non-object
    even if i check for existing attrs and achors i get no results.
    I believe the error is from this portion of the page
    li><span><a href="" title="Gumtree">London</a></span></li>
    		<li><a href="" class="change_city">Change city</a></li>
     <!-- It outputs this tag with attr, and in the next object i get the error -->
    <div id="community_links">
    	<span>Join the Gumtree Community!</span>
    		<li class="first" id="community_user"></li>
    		<li class="first" id="community_logout"></li>
    So anyway to work around this error with properties like hasAttribute ?

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    0 Post(s)
    0 Thread(s)
    Your problem is that you assume every <li> element contains at least one <a> element. You are assuming it here
    PHP Code:
    What if the <li> has no <a> tags as a child? Item(0) won't exist. You can't expect to use something that doesn't exist.

    Do you only want to find <a> elements if they're a child of an <li> tag?
    if so
    PHP Code:
    if ($value->length 0) {
    // there's at least one <a> tag in this <li>, so we can proceed

    If the <li> may contain more than 1 <a> tag, you better use a loop instead of assuming there's only one.

    If you just want all the <a> tags, and don't care whther or not they're a child of an <li>, then ditch the code involving the <li> and just use getElementsByTagName('a')
    PHP Code:
    foreach ($html->getElementsByTagName("a") as $element) {
    // read the attributes


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts