simonp Posted July 8, 2010 Share Posted July 8, 2010 Hi, I need to get some information from an XML feed into variables I can use in PHP eg: http://ws.geonames.org/findNearbyPlaceName?lat=50.36443&lng=-4.15639 outputs: <?xml version="1.0" encoding="UTF-8" standalone="no" ?> - <geonames> - <geoname> <toponymName>Plymouth</toponymName> <name>Plymouth</name> <lat>50.37153</lat> <lng>-4.14305</lng> <geonameId>2640194</geonameId> <countryCode>GB</countryCode> <countryName>United Kingdom</countryName> <fcl>P</fcl> <fcode>PPL</fcode> <distance>1.232</distance> </geoname> </geonames> How can extract <name> into $name and <countryName> into $countryname Cheers Simon Quote Link to comment https://forums.phpfreaks.com/topic/207119-getting-xml-data-into-variables/ Share on other sites More sharing options...
Mchl Posted July 8, 2010 Share Posted July 8, 2010 simplexml Quote Link to comment https://forums.phpfreaks.com/topic/207119-getting-xml-data-into-variables/#findComment-1082943 Share on other sites More sharing options...
simonp Posted July 8, 2010 Author Share Posted July 8, 2010 Hi Mchl, I've had a play with that and it will spit out the below but how do I get countryName into a variable (I've not had a lot of experience with arrays) Cheers Simon Array ( [name] => geonames [value] => [attr] => Array ( ) [children] => Array ( [geoname] => Array ( [name] => geoname [value] => [attr] => Array ( ) [children] => Array ( [toponymName] => Array ( [name] => toponymName [value] => Longport [attr] => Array ( ) [children] => Array ( ) ) [name] => Array ( [name] => name [value] => Longport [attr] => Array ( ) [children] => Array ( ) ) [lat] => Array ( [name] => lat [value] => 53.03333 [attr] => Array ( ) [children] => Array ( ) ) [lng] => Array ( [name] => lng [value] => -2.21667 [attr] => Array ( ) [children] => Array ( ) ) [geonameId] => Array ( [name] => geonameId [value] => 2643648 [attr] => Array ( ) [children] => Array ( ) ) [countryCode] => Array ( [name] => countryCode [value] => GB [attr] => Array ( ) [children] => Array ( ) ) [countryName] => Array ( [name] => countryName [value] => United Kingdom [attr] => Array ( ) [children] => Array ( ) ) [fcl] => Array ( [name] => fcl [value] => P [attr] => Array ( ) [children] => Array ( ) ) [fcode] => Array ( [name] => fcode [value] => PPL [attr] => Array ( ) [children] => Array ( ) ) [distance] => Array ( [name] => distance [value] => 0.962 [attr] => Array ( ) [children] => Array ( ) ) ) ) ) ) Quote Link to comment https://forums.phpfreaks.com/topic/207119-getting-xml-data-into-variables/#findComment-1082948 Share on other sites More sharing options...
Mchl Posted July 8, 2010 Share Posted July 8, 2010 Show the code you used to get this result and we'll work from there. Quote Link to comment https://forums.phpfreaks.com/topic/207119-getting-xml-data-into-variables/#findComment-1082957 Share on other sites More sharing options...
simonp Posted July 8, 2010 Author Share Posted July 8, 2010 Cheers - I'm using: <?php function xml2array($xml) { $arXML=array(); $arXML['name']=trim($xml->getName()); $arXML['value']=trim((string)$xml); $t=array(); foreach($xml->attributes() as $name => $value) $t[$name]=trim($value); $arXML['attr']=$t; $t=array(); foreach($xml->children() as $name => $xmlchild) $t[$name]=xml2array($xmlchild); $arXML['children']=$t; return($arXML); } $xml = simplexml_load_file('http://ws.geonames.org/findNearbyPlaceName?lat=53.039316&lng=-2.22706'); echo '<pre>'; print_r(xml2array($xml)); echo '</pre>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/207119-getting-xml-data-into-variables/#findComment-1082976 Share on other sites More sharing options...
Mchl Posted July 8, 2010 Share Posted July 8, 2010 Why don't you try like this? <?php $xml = simplexml_load_file('http://ws.geonames.org/findNearbyPlaceName?lat=53.039316&lng=-2.22706'); echo $xml->geoname->countryName; Quote Link to comment https://forums.phpfreaks.com/topic/207119-getting-xml-data-into-variables/#findComment-1082983 Share on other sites More sharing options...
simonp Posted July 8, 2010 Author Share Posted July 8, 2010 Thanks - I was so close - I tried: echo $xml->geonameS->countryName; Thanks again. Simon Quote Link to comment https://forums.phpfreaks.com/topic/207119-getting-xml-data-into-variables/#findComment-1082984 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.