Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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:
VB
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
                )

        )

)

I tried the following code to get the lat and long values but with no success:
PHP
$lat = $data->place[0]->lat;
$lon = $data->place[0]->lon;

Any suggestions?

Thank you,
Radu-Stefan
Posted

1 solution

Hi,

Seems like the following code works:
$lat = strval($data->place[0]->attributes()->lat);
$lon = strval($data->place[0]->attributes()->lon);

Thank you,
Radu-Stefan
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900