【问题标题】:parsing xml data from google maps api从谷歌地图api解析xml数据
【发布时间】:2013-05-08 23:45:14
【问题描述】:

我必须仅从带有我指定坐标的 xml 数据中获取方向信息。这是我的代码

    <?php 
$string= simplexml_load_file("http://maps.googleapis.com/maps/api/directions/xml?origin=37.036543,35.288086&destination=36.997415,35.288067&sensor=false");
    print_r ($string);
    $xml = new SimpleXMLElement($string);

    $result = $xml->xpath('/WebServiceRequest/result[2]/message/text()');

    while(list( , $node) = each($result)) {
        echo 'b/c: ',$node,"\n";
    }
    ?>

但我收到以下错误: 警告:SimpleXMLElement::__construct() [simplexmlelement.--construct]:实体:第 4 行:解析器错误:需要开始标记,在

中找不到“当然还有其他错误。

【问题讨论】:

    标签: php xml google-maps-api-3


    【解决方案1】:

    您可以执行以下操作。您编写的 xpath 我在您来自 Google 的响应 XML 中没有找到类似的东西。所以我根据我从谷歌收到的回复给出了提示。我假设您需要 &lt;step&gt; 元素及其子元素来实现/解决您的要求。在下面,您可以根据您的进一步要求进行必要的更改,下面就可以运行了。

    我还为您的调试目的提供了print_r($item)。所以也来看看吧。

    <?php 
    $xml = simplexml_load_file("http://maps.googleapis.com/maps/api/directions/xml?origin=37.036543,35.288086&destination=36.997415,35.288067&sensor=false");
    $result = $xml->xpath('//step');
    
    foreach($result as $item){
        //echo "<pre>";print_r($item);
        echo "travel_mode::".$item->travel_mode;
        echo "<br/>start_location::";
            //child elements of start_location element
            foreach($item->start_location as $child)
            {
                echo "<br/>lat::".$child->lat;
                echo "<br/>lng::".$child->lng;
            }
            //like above you can get other elements as well their child elements too.
        echo "<hr/>";
    }
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-30
      • 1970-01-01
      相关资源
      最近更新 更多