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 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试