使用implexML_LOAD_FILE解析XML文件时返回空对象

2022-04-13 00:00:00 xml php simplexml
 <?xml version="1.0" ?> 
  <aws:UrlInfoResponse xmlns:aws="http://alexa.amazonaws.com/doc/2005-10-05/">
   <aws:Response xmlns:aws="http://awis.amazonaws.com/doc/2005-07-11">
    <aws:OperationRequest>
    </aws:OperationRequest>
    <aws:UrlInfoResult>
     <aws:Alexa>
      <aws:TrafficData>
       <aws:DataUrl type="canonical">google.com/</aws:DataUrl> 
       <aws:Rank>1</aws:Rank> 
      </aws:TrafficData>
     </aws:Alexa>
    </aws:UrlInfoResult>
    <aws:ResponseStatus xmlns:aws="http://alexa.amazonaws.com/doc/2005-10-05/">
     <aws:StatusCode>Success</aws:StatusCode> 
    </aws:ResponseStatus>
   </aws:Response>
  </aws:UrlInfoResponse>

我要解析的只是该XML文档中的排名。 我所做的是...

        $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:wampwwwSEO Stats[Only Testing]Toolsweb_worth.php on line 13

错误在此行

 echo $xml->Response->UrlInfoResult->Alexa->TrafficData->Rank;
 this line is referred as line no 13..

解决方案

解析器返回空对象,因为根据documentation,您必须指定前缀命名空间-例如,您希望使用以下内容:

simplexml_load_file('/path/to/file.xml', null, null, 'aws', true);

working example

相关文章