我有这个错误:
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 ..请帮助我...
答案 0 :(得分:2)
您可以使用:
$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++;
}