Fonction attributes() PHP

Définition et utilisation

La fonction attributes() récupère les attributs de l'élément SimpleXML.

Cette fonction fournit les attributs et les valeurs définis dans une balise XML.

Syntaxe

class SimpleXMLElement
{
string attributes(ns,is_prefix)
}
Paramètres Description
ns Optionnel. L'espace de nom de l'attribut recherché.
is_prefix Optionnel. La valeur par défaut est false.

Exemple

Fichier XML :

<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>George</to>
<from>John</from>
<heading>Rappel</heading>
<body type="small" important="low">Ne oubliez pas la réunion !</body>
</note>

Code PHP :

<?php
$xml = simplexml_load_file("test.xml");
foreach($xml->body[0]->attributes() as $a => $b)
  {
  echo $a,'="',$b,'"';
  }
?>

Sortie :

type="small" important="low"