Php 从googlemapsapi解析xml数据
我必须仅从具有指定坐标的xml数据中获取方向信息。这是我的密码Php 从googlemapsapi解析xml数据,php,xml,google-maps-api-3,Php,Xml,Google Maps Api 3,我必须仅从具有指定坐标的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 Si
<?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";
}
?>
xpath('/WebServiceRequest/result[2]/message/text());
while(列表(,$node)=每个($result)){
回显“b/c:”,$node,“\n”;
}
?>
但我得到了以下错误:
警告:SimpleXMLElement::u construct()[SimpleXMLElement.-construct]:实体:第4行:解析器错误:应为开始标记,您可以执行以下实现。您编写的xpath我在您来自Google的响应XML中没有找到类似的内容。所以我根据谷歌的回复给出了提示。我假设您需要
元素及其子元素来实现/解决您的需求。在下文中,您可以根据进一步的要求进行必要的更改,下文已准备好运行
我还将打印($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/>";
}
?>
xpath('//step');
foreach($result作为$item){
//回显“;打印(项目);
回显“差旅模式::”$item->差旅模式;
回声“
开始位置::”;
//起始位置元素的子元素
foreach($item->start\u位置为$child)
{
echo“
lat::”$child->lat;
echo“
lng::”$child->lng;
}
//像上面一样,您也可以获得其他元素及其子元素。
回声“
”;
}
?>