Jump to content

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

?> 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.