dtlygweb2017 2012-11-19 14:20
浏览 21
已采纳

为什么我的XML数组无法调用?

I'm trying to get call an XML array using PHP, but for whatever reason, it is not providing results properly. The XML looks like this as a simpleXMLElement.

SimpleXMLElement Object ( [@attributes] => Array ( [copyright] => All data copyright 2012. )

[route] => SimpleXMLElement Object
    (
        [@attributes] => Array
            (
                [tag] => 385
                [title] => 385-Sheppard East
                [color] => ff0000
                [oppositeColor] => ffffff
                [latMin] => 43.7614499
                [latMax] => 43.8091799
                [lonMin] => -79.4111
                [lonMax] => -79.17073
            )

        [stop] => Array
            (
                [0] => SimpleXMLElement Object
                    (
                        [@attributes] => Array
                            (
                                [tag] => 14798
                                [title] => Sheppard Ave East At Yonge St (Yonge Station)
                                [lat] => 43.7614499
                                [lon] => -79.4111
                                [stopId] => 15028
                            )

                    )
              [1] => SimpleXMLElement Object
                    (
                        [@attributes] => Array
                            (
                                [tag] => 4024
                                [title] => Sheppard Ave East At Doris Ave
                                [lat] => 43.7619499
                                [lon] => -79.40842
                                [stopId] => 13563
                            )

                    )

There are several parts to the stop array. My code looks like this:

$url = "this_url";
$content = file_get_contents($url);
$xml = new SimpleXMLElement($content);
$route_array = $xml->route->stop;

When I print the $route_array, it only shows 1 record from the stops. Do I need to run this through a loop? Normally when I do this in JSON, it works fine. I'd like to get everything in the stop array only.

Thanks in advance to all of you experts out there helping out a beginner like myself

  • 写回答

1条回答 默认 最新

  • doutuzhuohao6449 2012-11-19 14:27
    关注

    Using print_r on SimpleXML elements does not always give you the full picture. Your elements are there but aren't shown.

    $xml->route->stop is an array of <stop> tags within <route>. So if you want to loop through each stop tag then:

    foreach($xml->route->stop as $stop)
    {
        echo (string)$stop; // prints the value of the <stop> tag
    }
    

    In the loop, $stop is a SimpleXML element, so in order to print its value you can cast the whole element as a string using the (string) syntax. You can still access attributes and other SimpleXML element properties.

    If you know which <stop> element you want to target, then you can get it directly:

    echo (string)$xml->route->stop[1]; // prints the second <stop> value
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c