使用simplexml\u load\u file解析XML文件返回空对象
但我这样做是错误的,它说使用simplexml\u load\u file解析XML文件返回空对象,xml,simplexml,php,Xml,Simplexml,Php,但我这样做是错误的,它说 $xml= simplexml_load_file("path of the file saved"); echo $xml->Response->UrlInfoResult->Alexa->TrafficData->Rank; 错误在这一行 Notice: Trying to get property of non-object in C:\wamp\www\SEO Stats[Only Testing
$xml= simplexml_load_file("path of the file saved");
echo $xml->Response->UrlInfoResult->Alexa->TrafficData->Rank;
错误在这一行
Notice: Trying to get property of non-object in C:\wamp\www\SEO Stats[Only Testing]\Tools\web_worth.php on line 13
您可能得到的结果对象嵌套错误,因此
$xml->Response->urlinformesult->Alexa->TrafficData->Rank中的一个对象代码>为空。尝试echo
echo $xml->Response->UrlInfoResult->Alexa->TrafficData->Rank;
this line is referred as line no 13..
如果第一个回显没有返回任何内容,则检查您是否需要输出UrlInfoResponse
,或者需要先注册名称空间。好的,这太模糊了,但是您可以始终打印$xml值以查看它是否真的是一个对象,有时会出现路径错误的情况。
请尝试以下代码以查看xml文档是否已实际加载:
echo $xml->Response;
echo $xml->Response->UrlInfoResult;
// etc ...
这样,您应该可以看到xml文档的内容。解析器返回了一个空对象,因为根据,您必须指定前缀名称空间,因此,例如,您希望使用如下内容:
<?php
if (file_exists('/path/to/document.xml')) {
$xml = simplexml_load_file('/path/to/document.xml');
print_r($xml);
} else {
exit('Failed to open /path/to/document.xml.');
}
echo$xml->Response->UrlInfoResult->Alexa->TrafficData->Rank;只需打印r($xml);检查它的内容是什么给SimpleXMLElement对象()是的,它加载正确了a和打印($xml给)SimpleXMLElement对象(),我试过一个接一个地回显它们。。直到echo$xml->Response->UrlInfoResult它的显示在屏幕上什么都没有,当我尝试这样做时,echo$xml->Response->UrlInfoResult->Alexa;它出现了一个错误。。
<?php
if (file_exists('/path/to/document.xml')) {
$xml = simplexml_load_file('/path/to/document.xml');
print_r($xml);
} else {
exit('Failed to open /path/to/document.xml.');
}
simplexml_load_file('/path/to/file.xml', null, null, 'aws', true);