首页 文章

从xml(maps api)获取数据(邮政编码)并创建URL

提问于
浏览
0

我创建了一个获取gpslocation的android应用程序并创建了一个url:https://maps.googleapis.com/maps/api/geocode/xml?latlng=00.00,00.00&sensor=true

示例:https://maps.googleapis.com/maps/api/geocode/xml?latlng=42.120636,-72.568731&sensor=true

这返回(只是一部分):

<GeocodeResponse>
    <status>OK</status>
    <result>
        <type>street_address</type>
        <formatted_address>
            17 Dorchester Street, Springfield, Massachusetts 01109, United States
        </formatted_address>
        <address_component>
            <long_name>17</long_name>
            <short_name>17</short_name>
            <type> ...

我对这部分感兴趣: 17 Dorchester Street, Springfield, Massachusetts 01109, United States .

我想创建一个新的网址,其中包含拉链编号"01109",如http://mysite.com?process=01109并打开此网站 .

谁能帮我!

3 回答

  • 0

    因此,您链接到的XML实际上包含邮政编码 . 它是 address_component ,其子项 type 的值为 postal_code . 恕我直言,最简单的方法是XPath:

    $d = <<<HER
    <GeocodeResponse>
      <!-- yada... --> 
      <result>
        <!-- yada --> 
        <address_component>
            <long_name>01109</long_name>
            <short_name>01109</short_name>
            <type>postal_code</type>
        </address_component>
        <!-- yada --> 
       </result>
       <!-- yoda --> 
    </GeocodeResponse>
    HER;
    
    $doc = new DomDocument();
    $doc->loadXML($d);
    $path = new DomXPath($doc);
    /*
    the query translates->
       // = all nodes
       address_component = which have the type of 'address_component'
       type = the children of the address_component with the type 'type'   
       [text() = "postal_code"] = but only the type's with the value 'postal_code'
       /preceding-sibling = all nodes before this one in the same parent
       ::*[1] = the one most adjacent of the sibling
    */
    
    $p = $path->query(
      '//address_component/type[text() = "postal_code"]/preceding-sibling::*[1]');
    $newUrl = 'http://mysite.com?process='.$p->item(0)->nodeValue;
    print($newUrl);
    
  • 0

    要从googleapis地理编码响应中获取特定数据,您不仅需要按名称定位元素,还需要按内容定位元素 .

    这可以通过Xpath轻松完成 . 幸运的是,PHP中的SimpleXML扩展支持阅读这种常见的格式化XML文档 .

    首先是Xpath:

    <GeocodeResponse>
        <result>
                <type>street_address</type>
    

    <result> 元素是具有子 <type> 的元素,其节点值为 street_address . 在Xpath中,这表示为:

    /*/result/type[. = "street_address"]/..
    

    它的工作原理类似于ZIP代码 . 它是前一个 <result> 节点的 <address_component> 子节点,其子节点 <type> ,节点值为 postal_code ,并且是您想要的那个元素:

    address_component/type[. = "postal_code"]/..
    

    到目前为止,xpaths . 要使其运行,只需加载XML文档,执行路径并读出您感兴趣的值:

    $xml = simplexml_load_file($xmlFile);
    
    list($result) = $xml->xpath('/*/result/type[. = "street_address"]/..');
    list($postal_code) = $result->xpath('address_component/type[. = "postal_code"]/..');
    
    echo $result->formatted_address, "\nZIP: ", $postal_code->long_name, "\n";
    

    通过在您的问题中链接的XML记录,这将创建以下输出:

    17 Dorchester Street, Springfield, MA 01109, USA
    ZIP: 01109
    

    我希望这是有帮助的 .

  • 0

    您可以使用simplexml和xpath访问postal_code short_name . 这是一个有效的例子:

    $location = simplexml_load_file('https://maps.googleapis.com/maps/api/geocode/xml?latlng=42.120636,-72.568731&sensor=true');
    
    $postal_code_search = 
    $location->xpath('//result[type="street_address"]/address_component[type="postal_code"]/short_name');
    $postal_code = (string) $postal_code_search[0];
    

    请注意,必须显式地将值转换为字符串,因为simplexml将在打印时将数组或对象返回到变量而不是字符串(在测试时可能会造成混淆) .

相关问题