使用implexML_LOAD_FILE解析XML文件时返回空对象 [英] Parsing a XML file using simplexml_load_file return empty object

查看:27
本文介绍了使用implexML_LOAD_FILE解析XML文件时返回空对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 <?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

这篇关于使用implexML_LOAD_FILE解析XML文件时返回空对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆