我正在尝试从http://ratings.food.gov.uk/OpenDataFiles/FHRS523en-GB.xml访问xml数据,但似乎无法正确检索它。但我不认为它与数据结构有任何关系。如何检索和显示数据?
<?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)
执行:
print_r($xml);
输出:
SimpleXMLElement Object
(
[Header] => SimpleXMLElement Object
(
[ExtractDate] => 2015-04-25
[ItemCount] => 1881
[ReturnCode] => Success
)
[EstablishmentCollection] => SimpleXMLElement Object
(
[EstablishmentDetail] => Array
(
//...
您将看到xml的结构,您将看到必须将foreach标题更改为:
foreach($xml->EstablishmentCollection->EstablishmentDetail as $data)
修改强>
同样如comments中的@hakre所述,您应该使用asXML()
:
echo $xml->asXML();
查看完整的整个xml。