嘿伙计们。我正在尝试解析 xml 文件,以便从中提取一些数据并将其显示在网站上。但是,当我使用不以 .xml
扩展名结尾的 url 时,我无法对结果运行 foreach
循环。换句话说,当我使用这种网址时:
$url = http://www.example.com/some_xml_file.xml
我可以在它上面运行一个 foreach 循环,它的工作方式就像魅力一样。但这一次我必须处理这种类型的网址:
$url = http://www.example.com/?some_var=something&some=something
所以你可以清楚地看到它不包含 .xml
扩展名。它返回解析良好的结果,但我无法让 foreach 循环处理该结果,因此我无法从中提取任何数据。你能帮我解决这个问题吗?
我正在做的事情的示例:
$url = 'http://www.example.com/?some_var=something&some=somethinge';
$sx = simplexml_load_file($url);
foreach ($sx->response as $row)
{
echo $row['status'];
}
这是 var_dump($sx) 的结果:
object(SimpleXMLElement)#334 (2) { ["@attributes"]=> array(2) { ["status"]=> string(2) "ok" ["version"]=> string(3) "1.0" } ["events"]=> object(SimpleXMLElement)#333 (2) { ["@attributes"]=> array(3) { ["resultCount"]=> string(1) "1" ["pageSize"]=> string(2) "15" ["pageIndex"]=> string(1) "0" } ["event"]=> object(SimpleXMLElement)#338 (10) { ["@attributes"]=> array(1) { ["id"]=> string(6) "180085" } ["name"]=> string(27) "Lady Gaga Monster Ball Tour" ["doordatetime"]=> string(19) "2010-11-01T18:30:00" ["tickets"]=> object(SimpleXMLElement)#337 (1) { ["ticket"]=> array(5) { [0]=> object(SimpleXMLElement)#339 (7) { ["@attributes"]=> array(1) { ["id"]=> string(6) "307902" } ["price"]=> string(7) "£55.00" ["url"]=> string(32) "http://www.gigjunkie.net/T307902" ["provider"]=> string(12) "Ticketmaster" ["soldout"]=> string(5) "false" ["ticketsavailable"]=> string(4) "true" ["issecondary"]=> string(5) "false" } [1]=> object(SimpleXMLElement)#340 (7) { ["@attributes"]=> array(1) { ["id"]=> string(6) "426248" } ["price"]=> string(7) "£82.00" ["url"]=> string(32) "http://www.gigjunkie.net/T426248" ["provider"]=> string(8) "Seatwave" ["soldout"]=> string(5) "false" ["ticketsavailable"]=> string(4) "true" ["issecondary"]=> string(4) "true" } [2]=> object(SimpleXMLElement)#437 (6) { ["@attributes"]=> array(1) { ["id"]=> string(6) "306230" } ["url"]=> string(32) "http://www.gigjunkie.net/T306230" ["provider"]=> string(7) "Viagogo" ["soldout"]=> string(5) "false" ["ticketsavailable"]=> string(4) "true" ["issecondary"]=> string(4) "true" } [3]=> object(SimpleXMLElement)#438 (6) { ["@attributes"]=> array(1) { ["id"]=> string(6) "325819" } ["url"]=> string(32) "http://www.gigjunkie.net/T325819" ["provider"]=> string(15) "WorldTicketShop" ["soldout"]=> string(5) "false" ["ticketsavailable"]=> string(4) "true" ["issecondary"]=> string(4) "true" } [4]=> object(SimpleXMLElement)#342 (6) { ["@attributes"]=> array(1) { ["id"]=> string(6) "402593" } ["url"]=> string(32) "http://www.gigjunkie.net/T402593" ["provider"]=> string(14) "Empire Tickets" ["soldout"]=> string(5) "false" ["ticketsavailable"]=> string(4) "true" ["issecondary"]=> string(4) "true" } } } ["venue"]=> object(SimpleXMLElement)#336 (9) { ["@attributes"]=> array(1) { ["id"]=> string(4) "1479" } ["name"]=> string(13) "Odyssey Arena" ["street"]=> string(13) "2 Queens Quay" ["town"]=> string(7) "Belfast" ["nameandtown"]=> string(22) "Odyssey Arena, Belfast" ["country"]=> string(2) "GB" ["gjurl"]=> string(90) "http://www.gigjunkie.net/gigs/Lady-Gaga-Monster-Ball-Tour/Odyssey-Arena/01-Nov-2010/180085" ["latitude"]=> string(9) "54.602158" ["longitude"]=> string(9) "-5.918215" } ["gjurl"]=> string(90) "http://www.gigjunkie.net/gigs/Lady-Gaga-Monster-Ball-Tour/Odyssey-Arena/01-Nov-2010/180085" ["artists"]=> object(SimpleXMLElement)#341 (1) { ["artist"]=> object(SimpleXMLElement)#332 (5) { ["@attributes"]=> array(2) { ["id"]=> string(5) "54194" ["isprimary"]=> string(4) "true" } ["name"]=> string(9) "Lady Gaga" ["gjurl"]=> string(42) "http://www.gigjunkie.net/artists/Lady-Gaga" ["thumbnailimage"]=> string(68) "http://images.gigjunkie.net/f5376fab-cd01-4432-9205-8a1d0be41437.jpg" ["mediumimage"]=> string(68) "http://images.gigjunkie.net/efbe624c-b63d-452a-a89b-221e846147ab.jpg" } } ["iscancelled"]=> string(5) "false" ["genre"]=> string(8) "Rock/Pop" ["image"]=> string(68) "http://images.gigjunkie.net/efbe624c-b63d-452a-a89b-221e846147ab.jpg" } } }
最佳答案
只需使用simplexml_load_file函数,如果无法解析xml,它将返回False
<?php
$url = 'http://www.example.com/?some_var=something&some=something'
if(simplexml_load_file($url)){
// is a xml
else {
// no xml found
}
?>
您可以在这里找到更多信息:http://www.php.net/manual/en/function.simplexml-load-file.php
关于php - 使用php解析xml文件时出现问题。 URL 地址不包含 .xml 扩展名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3941047/