如何在使用Dom Document时检查对象是否存在,因为我正在获取
错误“试图获取非对象的属性”
$getplacename = '//table//tr';
$getplacename2 = $xpath->query($getplacename);
//loop through each individual card
foreach($getplacename2 as $getplacename22) {
echo $getplacename22 ->childNodes->item(0)->childNodes->item(2)->textContent;
echo $getplacename22 ->childNodes->item(4)->textContent;
echo "<br /><br /><br />";
}
答案 0 :(得分:1)
嗯,我认为最简单的方法是将代码更改为类似的内容(假设错误发生在以下行之一):
foreach($getplacename2 as $getplacename22) {
if ($getplacename22->childNodes->item(0) != null && $getplacename22->childNodes->item(0)->childNodes->item(2) != null) {
echo $getplacename22->childNodes->item(0)->childNodes->item(2)->textContent;
}
if($getplacename22 ->childNodes->item(4) != null) {
echo $getplacename22->childNodes->item(4)->textContent;
}
echo "<br /><br /><br />";
}