Parsing a XML file using simplexml_load_file return empty object

12,656

The parser was returning an empty object because according to the documentation you have to specify the prefixed namespace - so for example you want to use something like:

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

working example

Share:
12,656
ravindar.dev
Author by

ravindar.dev

Updated on June 25, 2022

Comments

  • ravindar.dev
    ravindar.dev over 1 year
     <?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>
    

    All i want to parse is rank from this xml document. while trying so what iam doing is ...

            $xml= simplexml_load_file("path of the file saved");
            echo $xml->Response->UrlInfoResult->Alexa->TrafficData->Rank;
    

    but i am getting a error doing so .it says

     Notice: Trying to get property of non-object in C:\wamp\www\SEO Stats[Only Testing]\Tools\web_worth.php on line 13
    

    the error is in this line

     echo $xml->Response->UrlInfoResult->Alexa->TrafficData->Rank;
     this line is referred as line no 13..
    
  • ravindar.dev
    ravindar.dev over 10 years
    yes it's loading properlya and print_r($xml gives) SimpleXMLElement Object ( )
  • ravindar.dev
    ravindar.dev over 10 years
    i did try echo them one by one .. Till echo $xml->Response->UrlInfoResult its show's nothing on screen and when i try to do this echo $xml->Response->UrlInfoResult->Alexa; It come up with a error..