[SimpleXML] Récupération de valeurs dans une boucle Foreach

Eléphanteau du PHP | 19 Messages

09 déc. 2007, 22:08

Bonjour,


je souhaiterais récupérer les données d'un flux dans une boucle du type 'Foreach' car je ne sais pas de combien d'éléments est composé ce flux. J'ai réalisé le code ci-dessous mais rien ne s'affiche.
$xml_cat = simplexml_load_file($flux);
$i=1;
foreach($xml_cat->ArrayOfString as $element) {
echo '$element->string[$i]';
$i++;
}
Voici un extrait du flux XML :

Code : Tout sélectionner

<ArrayOfString xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.sonnerie.com"> <string>Nouveautés</string> <string>Hits des Ventes</string> <string>Hits Radio</string> <string>Hits Rap/Rnb</string> </ArrayOfString>

José

Administrateur PHPfrance
Administrateur PHPfrance | 3088 Messages

09 déc. 2007, 22:46

Si ce que tu veux faire, c'est une boucle qui traite chaque <string> alors la boucle devrait dire : "pour chaque string", ou en anglais "for each string". En PHP, ça donne
foreach($xml_cat->ArrayOfString->string as $string)
{
    echo $string;
}

Eléphanteau du PHP | 19 Messages

10 déc. 2007, 01:13

Bonne idée mais le problème c'est que ça me renvoi une erreur :

'Invalid argument supplied for foreach()'

Administrateur PHPfrance
Administrateur PHPfrance | 3088 Messages

10 déc. 2007, 01:38

Chez moi ça marche, regarde ce qui diffère chez toi
$root = simplexml_load_string(
'<root>
	<ArrayOfString xmlns:xsd="http://www.w3.org/2001/XMLSchema"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.sonnerie.com">
		  <string>Nouveautés</string>
		  <string>Hits des Ventes</string>
		  <string>Hits Radio</string>
		  <string>Hits Rap/Rnb</string>
	</ArrayOfString>
</root>'
);

foreach($root->ArrayOfString->string as $string) 
{ 
    echo $string, "\n"; 
}

Mammouth du PHP | 2937 Messages

10 déc. 2007, 16:17

J'ai réalisé le code ci-dessous mais rien ne s'affiche.
$xml_cat = simplexml_load_file($flux);
$i=1;
foreach($xml_cat->ArrayOfString as $element) {
echo '$element->string[$i]';
$i++;
}
Voici un extrait du flux XML :

Code : Tout sélectionner

<ArrayOfString xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.sonnerie.com"> <string>Nouveautés</string> <string>Hits des Ventes</string> <string>Hits Radio</string> <string>Hits Rap/Rnb</string> </ArrayOfString>
Le code du flux XML tel quel me fait dire que l'élément ArrayOfString est l'élément racine de ton flux. Par conséquent, $xmlcat -> ArrayOfString ne marchera jamais, puisque la variable $xmlcat, telle qu'elle est définie, sélectionne déjà l'élément racine. Autrement dit,
$xml_cat = simplexml_load_file ($flux);
// Sélection des attributs de l'élément racine
echo $xml_cat['xmlns:xsd']; // Affiche l'espace de nom XSD (http://www.w3.org/2001/XMLSchema)
echo $xml_cat['xmlns:xsi']; // Affiche l'espace de nom XSI (http://www.w3.org/2001/XMLSchema-instance)
echo $xml_cat['xmlns']; // Affiche l'espace de nom de ton flux (http://www.sonnerie.com)

// Sélection des éléments string
foreach ($xml_cat -> string as $element)
{
  echo htmlspecialchars ((string) $element); // Affiche le contenu de chaque élément string (en prenant soin d'échapper les &, < et > par leur entité HTML respective)
}