2013-05-08 95 views
-1

我必须从指定坐标的xml数据中获取方向信息。这里是我的代码解析来自Google地图api的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 :: __结构()的SimpleXMLElement .--构建]:实体:行4:分析器错误:开始标记预期, '<'找不到

当然还有其余的错误。

回答

0

你可以做下面的实现。您所写的xpath在Google的响应XML中找不到类似的内容。所以我根据我收到Google的回应给出提示。我假设你需要<step>元素及其子项来实现/解决你的需求。在下面,您可以根据您的进一步要求进行必要的更改,并且下面已准备好运行。

我也把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/>"; 
} 
?> 
+0

谢谢你的帮助。这很好。 – BKadmin 2013-05-09 12:06:09