Dennis de Wit
Dennis de Wit

Reputation: 21

Iterating over XML data in PHP using SimpleXML

I have the following Simple XML Data:

  [Listeners] => 37
            [listener] => Array
                (
                    [0] => SimpleXMLElement Object
                        (
                            [IP] => 0.0.0.1
                            [UserAgent] => curl/7.19.7 (i386-redhat-linux-gnu) libcurl/7.19.7 NSS/3.16.2.3 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2
                            [Connected] => 1236
                            [ID] => 120
                        )

                    [1] => SimpleXMLElement Object
                        (
                            [IP] => 0.0.0.2
                            [UserAgent] => curl/7.19.7 (i386-redhat-linux-gnu) libcurl/7.19.7 NSS/3.16.2.3 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2
                            [Connected] => 1235
                            [ID] => 121
                        )

I want to show [IP].

So I have this PHP script:

<?php

$url = "http://admin:[email protected]/admin/listclients?mount=/FavoriteFM";
$xml = simplexml_load_file($url);
echo "<pre>";
print_r($xml);
 $IP = $xml->listener->IP;
 echo $IP[0];

?>

But it's not working. How can I display display IP in the proper way?

Upvotes: 2

Views: 98

Answers (3)

Dennis de Wit
Dennis de Wit

Reputation: 21

This should work for you:

foreach($xml->source->listener as $ip) 
    echo $ip->IP . "<br />"; 

Upvotes: 0

rudra panchal
rudra panchal

Reputation: 11

$url = "http://admin:[email protected]/admin/listclients?mount=/FavoriteFM";
$xml = simplexml_load_file($url);
$IP = $xml->source->listener;
    foreach($IP as $row){
      echo $row->IP;
    }

You will get all the IPs of array.

Upvotes: 1

Rizier123
Rizier123

Reputation: 59681

Just change this line:

(You simply forgot the first level source)

$IP = $xml->listener->IP;

to this:

$IP = $xml->source->listener->IP;
        //^^^^^^^^ See here

Upvotes: 1

Related Questions