-1

I need to access attribute country's value (without using xpath) from the following: http://apps.db.ripe.net/whois/lookup/ripe/inetnum/79.6.54.99.xml

This is what I have done so far

$xml = simplexml_load_file("http://apps.db.ripe.net/whois/lookup/ripe/inetnum/79.6.54.99.xml");
$country = $xml->objects->object->attributes->attribute ... ???
wlf
  • 851
  • 1
  • 9
  • 15

3 Answers3

2
$xml = simplexml_load_file('http://apps.db.ripe.net/whois/lookup/ripe/inetnum/79.6.54.99.xml');
foreach ($xml->objects->object->attributes->attribute as $attr) {
   if ($attr->attributes()->name == 'country') {
      echo $attr->attributes()->value;
   }
}
hohner
  • 11,498
  • 8
  • 49
  • 84
0

I have just found two ways to do it, with [ ] and with attributes().

foreach(simplexml_load_file("http://apps.db.ripe.net/whois/lookup/ripe/inetnum/79.6.54.99.xml")->objects->object->attributes->attribute as $a){
if($a['name'] == 'country')
if(in_array($a['value'],array('IT'))) exit;
else break;
}

I'll leave this question open till tomorrow just in case anyone else has something up his/her sleeve.

wlf
  • 851
  • 1
  • 9
  • 15
0

This works;

$s = simplexml_load_file("http://apps.db.ripe.net/whois/lookup/ripe/inetnum/79.6.54.99.xml");
foreach ($s->objects->object->attributes->attribute as $attr) {
    $attrs = $attr->attributes();
    if ((string) $attrs->name == "country") {
        $country = (string) $attrs->value;
        break;
    }
}
print $country; // IT

But there is an option too, if it's suitable for you;

$s = file_get_contents("http://apps.db.ripe.net/whois/lookup/ripe/inetnum/79.6.54.99.xml");
preg_match_all('~<attribute\s+name="country"\s+value="(.*?)".*?/>~i', $s, $m);
print_r($m);

Out;

Array
(
    [0] => Array
        (
            [0] => <attribute name="country" value="IT"/>
        )

    [1] => Array
        (
            [0] => IT
        )

)
Kerem
  • 11,377
  • 5
  • 59
  • 58