试图获取非对象的属性,变量是PHP中的XML语句
我试图将XML文件中的值分配给嵌套for循环中的变量。该值在html文件中的输出是正确的,但每次遍历代码时,它仍然为每一行提供“尝试获取非对象的属性” 我的代码:试图获取非对象的属性,变量是PHP中的XML语句,php,xml,Php,Xml,我试图将XML文件中的值分配给嵌套for循环中的变量。该值在html文件中的输出是正确的,但每次遍历代码时,它仍然为每一行提供“尝试获取非对象的属性” 我的代码: $drName=(string)$xml->Device[$lDev]->Drives->Drive[$n]->Name; $drBadSect=(string)$xml->Device[$lDev]->Drives->Drive[$n]->BadSectors; $drPwrHrs=(
$drName=(string)$xml->Device[$lDev]->Drives->Drive[$n]->Name;
$drBadSect=(string)$xml->Device[$lDev]->Drives->Drive[$n]->BadSectors;
$drPwrHrs=(string)$xml->Device[$lDev]->Drives->Drive[$n]->PowerOnHours;
$drPwrCycle=(string)$xml->Device[$lDev]->Drives->Drive[$n]->PowerCycleCount;
$drTemp=(string)$xml->Device[$lDev]->Drives->Drive[$n]->Temperature;
$drError=(string)$xml->Device[$lDev]->Drives->Drive[$n]->Error;
XML文件的这一特定部分:
<Drives total="1">
<Drive>
<Name>sda</Name>
<BadSectors>0</BadSectors>
<PowerOnHours>3869</PowerOnHours>
<PowerCycleCount>10</PowerCycleCount>
<Temperature>0</Temperature>
<Error>0</Error>
</Drive>
但就像那样,我最终出现了一个解析错误,出乎意料[意料]
除了更改php intrepeter的警报级别之外,还有什么其他选项?向我们展示您的全部代码,包括循环。例如,什么是
设备[$lDev]
?如果元素不在XML中(并且没有转换为对象/属性),SimpleXML将抛出通知。如果你不确定某个元素是否总是存在,你必须在阅读之前检查它。整个文件有点大(大约150行),所以有一个$lDev是遍历xml文件的循环变量。Device[]是我正在监视的设备阵列。
$drName=(string)$xml[Device[$lDev]][Drives][Drive[$n]][Name];