To get an attribute in the node, use node->attributes()->attributeName
SimpleXMLElement::attributes
(PHP 5 >= 5.0.1)
SimpleXMLElement::attributes — Identifie les attributs d'un élément
Description
SimpleXMLElement attributes
([ string $ns
[, bool $is_prefix
]] )
Fournit les attributs et les valeurs définies dans une balise XML.
Note: SimpleXML ajoute des propriétés itératives pour presque toutes ses méthodes. Celles-ci ne peuvent être vues en utilisant var_dump() ou tout autre fonction qui examine les objets.
Liste de paramètres
- ns
-
Un espace de noms optionnel pour les attributs récupérés
- is_prefix
-
Par défaut, vaut FALSE
Valeurs de retour
Exemples
Exemple #1 Interprétation d'une chaîne XML
<?php
$string = <<<XML
<a>
<foo name="one" game="lonely">1</foo>
</a>
XML;
$xml = simplexml_load_string($string);
foreach($xml->foo[0]->attributes() as $a => $b) {
echo $a,'="',$b,"\"\n";
}
?>
L'exemple ci-dessus va afficher :
name="one" game="lonely"
SimpleXMLElement::attributes
gillllberg at gmail dot com
04-Nov-2009 10:21
04-Nov-2009 10:21
skerr at mojavi dot org
10-Dec-2004 06:55
10-Dec-2004 06:55
You can also access the node as an array to get attributes:
<?php
$xml = simplexml_load_file('file.xml');
echo 'Attribute: ' . $xml['attribute'];
?>
inge at elektronaut dot no
26-May-2004 05:53
26-May-2004 05:53
here's a simple function to get an attribute by name, based on the example
<?php
function findAttribute($object, $attribute) {
foreach($object->attributes() as $a => $b) {
if ($a == $attribute) {
$return = $b;
}
}
if($return) {
return $return;
}
}
?>
