0

I'm trying to access the xml data from http://ratings.food.gov.uk/OpenDataFiles/FHRS523en-GB.xml but can't seem to retrieve it correctly. I don't think it has anything to do with the structure of the data however. How can I retrieve and display the data?

<?php
$fhrsid = 'FHRSID';

$LocalAuthorityBusinessID = 'LocalAuthorityBusinessID';

$xml = simplexml_load_file("http://ratings.food.gov.uk/OpenDataFiles/FHRS523en-GB.xml");
echo "<h2>".$xml->getName()."</h2><br />";

foreach($xml->children() as $data)

{
    echo "FHRSID: ".$data->$fhrsid."<br />";
    echo "LocalAuthorityBusinessID : ".$data->$LocalAuthorityBusinessID." <br />";
    echo "BusinessName : ".$data->BusinessName." <br />";
    echo "BusinessType : ".$data->BusinessType." <br />";
    echo "BusinessTypeID : ".$data->BusinessTypeID." <br />";
    echo "AddressLine1 : ".$data->AddressLine1." <br />";
    echo "AddressLine2 : ".$data->AddressLine2." <br />";
    echo "<hr/>";
}
?>
0

1 Answer 1

1

Do:

print_r($xml);

output:

SimpleXMLElement Object
(
    [Header] => SimpleXMLElement Object
        (
            [ExtractDate] => 2015-04-25
            [ItemCount] => 1881
            [ReturnCode] => Success
        )

    [EstablishmentCollection] => SimpleXMLElement Object
        (
            [EstablishmentDetail] => Array
                (
                    //...

And you will see the structure of your xml and you will see that you have to change your foreach header to this:

foreach($xml->EstablishmentCollection->EstablishmentDetail as $data)

EDIT:

Also as mentioned in the comments from @hakre, you should use asXML():

echo $xml->asXML();

To see the full and entire xml.

Sign up to request clarification or add additional context in comments.

3 Comments

Please also share the information that it won't show the whole picture and to see all XML, $xml->asXML() should be used.
@hakre You're right :) (I think there is even an answer about this with: You should never trust the output of print_r() or var_dump())
Especially not with SImpleXMLElement (or say a PDO connection when you expect that print_r then shows you the database data, or ... .). :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.