使用SimpleXML解析XML响应 [英] Parse XML response with SimpleXML

查看:92
本文介绍了使用SimpleXML解析XML响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我需要帮助从XML文件中检索一些数据。

我向URL发出请求我想要解析响应XML文件以获取lat和long值。

我做了什么:

Hi,

I need help with retrieving some data from a XML file.
I make a request to an URL and I want to parse the response XML file to get lat and long values.
What I did:

$xml = file_get_contents($url);
$data = new SimpleXMLElement($xml);



$数据变量包含以下内容:


$data variable contains the following:

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [timestamp] => Sat, 15 Jun 13 20:02:13 +0000
            [attribution] => Data Š OpenStreetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright
            [querystring] => Bucharest-Romania
            [polygon] => false
            [exclude_place_ids] => 331526
            [more_url] => http://nominatim.openstreetmap.org/search?format=xml&exclude_place_ids=331526&q=Bucharest-Romania
        )

    [place] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [place_id] => 331526
                    [osm_type] => node
                    [osm_id] => 96209423
                    [place_rank] => 15
                    [boundingbox] => 44.4361381530762,44.4361419677734,26.1027431488037,26.1027450561523
                    [lat] => 44.436139
                    [lon] => 26.1027436
                    [display_name] => București, Sector 2, Bucuresti, România, European Union
                    [class] => place
                    [type] => city
                    [importance] => 0.73231672860554
                    [icon] => http://nominatim.openstreetmap.org/images/mapicons/poi_place_city.p.20.png
                )

        )

)



我尝试使用以下代码来获取lat和long值,但没有成功:


I tried the following code to get the lat and long values but with no success:

$lat = $data->place[0]->lat;
$lon = $data->place[0]->lon;



有什么建议吗?



谢谢,

Radu-Stefan


Any suggestions?

Thank you,
Radu-Stefan

推荐答案

xml = file_get_contents(
xml = file_get_contents(


url);


data = new SimpleXMLElement(
data = new SimpleXMLElement(


这篇关于使用SimpleXML解析XML响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆