1

我有这个错误:

Error Trying to get property of non-object

代码是:

 $dom->preserveWhiteSpace = false; 

    $tables = $dom->getElementsByTagName('table'); 

    $rows = $tables->item(0)->getElementsByTagName('tr'); 

   $i=0; 
   foreach ($rows as $row) 
    { 
        /*** get each column by tag name ***/ 
        $cols = $row->getElementsByTagName('td'); 

        $this->data['Table'][$i] = array(
                           'Attrb1' => $cols->item(0)->nodeValue,
                           'Attrib2' => $cols->item(1)->nodeValue

                                  );
     $i++;

    } 
         }

        return $this->toArray();
    }

我有这个错误的行是:

'Attrib2' => $cols->item(1)->nodeValue

html代码是:

<table border=1 align="center" cellpadding=5 width="95%">
                                            <!doctype html public "-//w3c//dtd html 4.0//EN">
<html it>
<head>
<meta name="Generator" content="OLS">
</head>
<body>
<td colspan=2 align="center">
<b>
<i>
Attrib1
</i>
</b>
</td>
<td>
<b>
<i>
Attrib2
</i>
</b>
</td>
<td>
<b>
<i>
<tr>
<td>
A000211
</td>
<td nowrap>
Statistic
</td>
</tr>

但我不知道问题出在 foreach 中还是我所做的所有获取数据都是一个大错误,或者我以错误的方式处理 html..所以请帮助我..

4

1 回答 1

3

您可以使用:

$dom->loadHTML($result); 
libxml_clear_errors();
libxml_use_internal_errors($errors);
/*** discard white space ***/ 
$dom->preserveWhiteSpace = false; 
/*** the table by its tag name ***/ 
$tables = $dom->getElementsByTagName('table'); 
/*** get all rows from the table ***/ 
$rows = $tables->item(1)->getElementsByTagName('tr'); 
/*** loop over the table rows ***/ 
$i=0; 
foreach ($rows as $row) { 
    /*** get each column by tag name ***/ 
    $cols = $row->getElementsByTagName('td'); 

    $this->data['List'][$i] = array(
       'name1' => $cols->item(1)->nodeValue,
       'name2' => $cols->item(2)->nodeValue,
       'name3' => $cols->item(3)->nodeValue,
       'name4' => $cols->item(4)->nodeValue,
       'name5' => $cols->item(5)->nodeValue,
       'name6' => $cols->item(6)->nodeValue,
       'name7' => $cols->item(7)->nodeValue
    );
    $i++;
} 
于 2013-12-16T13:55:28.033 回答