2010-04-01 7 views
0

Voici mon XML:éléments @attribute en XML

["link"]=> 
      array(2) { 
      [0]=> 
      object(SimpleXMLElement)#244 (1) { 
       ["@attributes"]=> 
       array(3) { 
       ["type"]=> 
       string(9) "text/html" 
       ["href"]=> 
       string(48) "http://twitter.com/ddd/statuses/1226112723" 
       ["rel"]=> 
       string(9) "alternate" 
       } 
      } 
      [1]=> 
      object(SimpleXMLElement)#245 (1) { 
       ["@attributes"]=> 
       array(3) { 
       ["type"]=> 
       string(9) "image/png" 
       ["href"]=> 
       string(59) "http://a3.twimg.com/profile_images/226345673/ddd_normal.png" 
       ["rel"]=> 
       string(5) "image" 
       } 
      } 
      } 
      ["author"]=> 
      object(SimpleXMLElement)#246 (2) { 
      ["name"]=> 
      string(14) "ddd" 
      ["uri"]=> 
      string(16) "http://ddd.co.uk" 
      } 

Le XML ci-dessus fait partie d'un ensemble plus grand, mais comment puis-je le

href ->   ["href"]=> 
        string(59) "http://a3.twimg.com/profile_images/226345673/ddd_normal.png" 

Element faire écho en PHP?

C'est ce que j'ai essayé (Mauvais je sais)

foreach ($entry->link as $img) { 
      echo $img->href; 
      echo "<img src='". $img ."' />"; 
      } 

Répondre

3

Pour accéder aux attributs d'un nœud, vous devez utiliser la fonction attributes():

$xml = simplexml_load_string($string); 
foreach($xml->foo[0]->attributes() as $a => $b) { 
    echo $a,'="',$b,"\"\n"; 
} 

En utilisant votre exemple:

foreach($entry->link as $link) { 
    echo $link->attributes()->href; 
} 
+0

+1 pour l'utilisation de mon exemple:) – CLiown