Funzione attributes() PHP

Definizione e uso

La funzione attributes() ottiene gli attributi dell'elemento SimpleXML.

La funzione fornisce gli attributi e i valori definiti in un tag XML.

Sintassi

class SimpleXMLElement
{
string attributes(ns,is_prefix)
}
Parametro Descrizione
ns Opzionale. Nome spazio dell'attributo cercato.
is_prefix Opzionale. Il valore predefinito è false.

Esempio

File XML:

<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>George</to>
<from>John</from>
<heading>Ricordatorio</heading>
<body type="small" important="low">Non dimenticare l'incontro!</body>
</note>

Codice PHP:

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

Output:

type="small" important="low"