我有一个xml feed,我需要从中访问一个元素。但是,我尝试过的方法都没有奏效。
以下是完整的xml Feed:
<GeocodeResponse>
<status>OK</status>
<result>
<type>locality</type>
<type>political</type>
<formatted_address>Liverpool, Merseyside, UK</formatted_address>
<address_component>
<long_name>Liverpool</long_name>
<short_name>Liverpool</short_name>
<type>locality</type>
<type>political</type>
</address_component>
<address_component>
<long_name>Merseyside</long_name>
<short_name>Mersyd</short_name>
<type>administrative_area_level_2</type>
<type>political</type>
</address_component>
<address_component>
<long_name>England</long_name>
<short_name>England</short_name>
<type>administrative_area_level_1</type>
<type>political</type>
</address_component>
<address_component>
<long_name>United Kingdom</long_name>
<short_name>GB</short_name>
<type>country</type>
<type>political</type>
</address_component>
<geometry>
<location>
<lat>53.4115400</lat>
<lng>-2.9901160</lng>
</location>
<location_type>APPROXIMATE</location_type>
<viewport>
<southwest>
<lat>53.3049930</lat>
<lng>-3.2462348</lng>
</southwest>
<northeast>
<lat>53.5178208</lat>
<lng>-2.7339972</lng>
</northeast>
</viewport>
<bounds>
<southwest>
<lat>53.3115426</lat>
<lng>-3.0191794</lng>
</southwest>
<northeast>
<lat>53.5039071</lat>
<lng>-2.8115043</lng>
</northeast>
</bounds>
</geometry>
</result>
</GeocodeResponse>
我正在尝试选择geometry-&gt; location-&gt; lat-&gt;位置,然后选择此lat和lng。
答案 0 :(得分:2)
您是否尝试过simplexml_load_string或simplexml_load_file? 只需使用它加载数据,然后访问所需的节点,如下所示:
$xml = simplexml_load_file('your.xml');
echo $xml->GeocodeResponse->result->geometry->location->lat;
echo $xml->GeocodeResponse->result->geometry->location->lon;
答案 1 :(得分:0)
如果你有一个字符串,你可以这样做:
$str = "<?xml version='1.0'?>
<document>
<cmd>login</cmd>
<login>Richard</login>
</document>";
$xml = simplexml_load_string($str);
print_r($xml);
print_r($xml->document->cmd);
// Remember to type cast it a string if you want the actual value.
// Example:
$myval = (string) $xml->document->cmd;