xml - SimpleXML selecting attributes with PHP? -


okay i've never worked simplexml before , i'm having few problems

here's php:

map.api.php

$location = $_session['location']; $address = $location; // business location $prepaddr = str_replace(' ','+',$address); $feedurl="http://nominatim.openstreetmap.org/search?q=". $prepaddr ."&format=xml&addressdetails=1&polygon=1";  $sxml = simplexml_load_file($feedurl);  foreach($sxml->attributes() $type){   $lat = $type->place['lat'];   $long = $type->place['lon']; } 

and here's example of xml table i'm working from.

<searchresults timestamp="thu, 28 jan 16 14:16:34 +0000" attribution="data © openstreetmap contributors, odbl 1.0. http://www.openstreetmap.org/copyright" querystring="m27 6bu, 149 station road, swinton, manchester" polygon="true" exclude_place_ids="65827001" more_url="http://nominatim.openstreetmap.org/search.php?format=xml&exclude_place_ids=65827001&accept-language=en-us,en;q=0.8&polygon=1&addressdetails=1&q=m27+6bu%2c+149+station+road%2c+swinton%2c+manchester">   <place place_id="65827001" osm_type="way" osm_id="32861649" place_rank="26" boundingbox="53.5122168,53.5190893,-2.3402445,-2.3331231" lat="53.5156919" lon="-2.3368185" display_name="station road, newtown, salford, greater manchester, north west england, england, m27 4ae, united kingdom" class="highway" type="secondary" importance="0.5">     <road>station road</road>     <suburb>newtown</suburb>     <town>salford</town>     <county>greater manchester</county>     <state_district>north west england</state_district>     <state>england</state>     <postcode>m27 4ae</postcode>     <country>united kingdom</country>     <country_code>gb</country_code>   </place> </searchresults> 

i want select "lat" , "lon" attributes <place>, when echo $lat , $long empty, why?

when call attributes did above, it's going act on first element, in case root. need call attributes on element want attributes for. easiest way be

$sxml->place[0]->attributes() 

does make sense? you're telling simplexml seek element want analyze, returning new simplexml object represents element's attributes. see if docs help

another option have using xpath return place elements, iterating through , calling attributes() on each element, in case have more 1 place.


Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -