Erreur alors que le script marche très bien

Résolu
canarder Messages postés 1714 Date d'inscription   Statut Membre Dernière intervention   -  
canarder Messages postés 1714 Date d'inscription   Statut Membre Dernière intervention   -
Bonjour, j'ai créé un petit script vite fait qui parse un xml :
<?php
//error_reporting(0);
$xml = "http://www.hordes.fr/xml?k=c98ddc0a7054458c0614d22d00be9536";
$dom = new DOMDocument();
if (!$dom->load($xml)) {
exit("erreur");
}
$citizen = $dom->getElementsByTagName("citizen");
$nbrCitoy = $citizen->length;
echo "<style>tr, th, td {border:1px solid black;}</style>";
echo "<table>\n<tr style=\"background:#FF00FF;\"><th>Joueur</th><th>Banni ?</th><th>Dehors ?</th><th>Maison</th></tr>\n";
for($i=0;$i<=$nbrCitoy;$i++) {
if($citizen->item($i)->getAttribute("name")=="MonsieurMechant" OR
$citizen->item($i)->getAttribute("name")=="ledragonducoin" OR
$citizen->item($i)->getAttribute("name")=="mad05" OR
$citizen->item($i)->getAttribute("name")=="ErEiCrOs" OR
$citizen->item($i)->getAttribute("name")=="dashing") {
echo "<tr>";
echo "<td>".$citizen->item($i)->getAttribute("name")."</td>";
if($citizen->item($i)->getAttribute("ban")=="0") echo "<td>Non</td>";
else echo "<td style=\"color:red;\">Oui</td>";
if($citizen->item($i)->getAttribute("out")=="1") echo "<td style=\"color:green;\">Oui</td>";
else echo "<td>Non</td>";
if($citizen->item($i)->getAttribute("baseDef")>1) echo "<td style=\"color:red;\">Taudis ou plus</td>";
elseif($citizen->item($i)->getAttribute("baseDef")==1) echo "<td style=\"color:green;\">Tente</td>";
else echo "<td>Normale</td>";
echo "</tr>\n";
}
}
echo "</table>";
?>


Mais cette erreur s'affiche (avant le tableau mais après dans le code html) :
Fatal error: Call to a member function getAttribute() on a non-object in *** on line 13


Mais le script marche très bien, c'est ça que je comprend pas. Je ne peux pas écrire après ma boucle for. Au pire je cache les erreurs avec error_reporting(0); (ligne 2), mais ça ne résolue pas l'erreur.

Merci pour votre aide.


A voir également:

1 réponse

Groarh Messages postés 682 Date d'inscription   Statut Membre Dernière intervention   185
 
Salut !
On s'est déjà vus à propos de Hordes je crois :)

Alors analysons l'erreur. La ligne 13 :
if($citizen->item($i)->getAttribute("name")=="MonsieurMechant" OR

Le problème est un appel à la méthode getAttribute() sur quelque chose qui n'est pas un objet, en l'occurence $citizen->item($i).

Ta boucle for fait un tour de trop : quand $i est égal à $nbCitoy, $citizen->item($i) renvoie null, du coup c'est comme si tu faisais null->getAttribute("name").
Remplace le <= par un < strict.

;)
0
canarder Messages postés 1714 Date d'inscription   Statut Membre Dernière intervention   355
 
en effet, avec <, il n'y a plus d'erreur. sauf qu'il ne prend pas en compte du dernier citizen, non ?
EDIT : ah non ça part de 0, c'est vrai ...
0