Jump to content

Getting XML data into variables?


simonp

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/207119-getting-xml-data-into-variables/
Share on other sites

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

                                        (

                                        )

 

                                )

 

                        )

 

                )

 

        )

 

)

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>';

?> 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.