Précédent   Forum des professionnels en informatique > PHP > Langage > Débuter
Débuter Forum d'entraide pour débuter en PHP. Avant de poster -> Cours PHP, FAQ PHP, Outils PHP, etc.
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse Proposer ce sujet en actualité
 
Outils de la discussion
Publicité
'
Vieux 07/02/2011, 18h55   #1
Invité de passage
 
Inscription : juin 2009
Messages : 23
Détails du profil
Informations forums :
Inscription : juin 2009
Messages : 23
Points : 2
Points : 2
Par défaut XML récupérer un élément particulier.

Bonjour,

J'aimerai récupérer l'élément correspondant à un id.
ex: je veux le libelle de ID 2, ça me retournerais : Albania.
Et faire ça sans faire de boucle biensur.

j'ai pour le moment :
Code :
1
2
3
 
$xml = simplexml_load_file($fichier);
$list_country = $xml->xpath('//country');
Voilà ce que me retourne var_dump($list_country) :
Code :
array(80) { [0]=> object(SimpleXMLElement)#2 (2) { ["id_country"]=> string(1) "2" ["libelle"]=> string(7) "Albania" } [1]=> object(SimpleXMLElement)#3 (2) { ["id_country"]=> string(1) "3" ["libelle"]=> string(7) "Algeria" } [2]=> object(SimpleXMLElement)#4 (2) { ["id_country"]=> string(1) "9" ["libelle"]=> string(9) "Argentina" } [3]=> object(SimpleXMLElement)#5 (2) { ["id_country"]=> string(2) "12" ["libelle"]=> string(9) "Australia" } [4]=> object(SimpleXMLElement)#6 (2) { ["id_country"]=> string(2) "13" ["libelle"]=> string(7) "Austria" } [5]=> object(SimpleXMLElement)#7 (2) { ["id_country"]=> string(2) "18" ["libelle"]=> string(8) "Barbados" } [6]=> object(SimpleXMLElement)#8 (2) { ["id_country"]=> string(2) "19" ["libelle"]=> string(7) "Belarus" } [7]=> object(SimpleXMLElement)#9 (2) { ["id_country"]=> string(2) "20" ["libelle"]=> string(7) "Belgium" } [8]=> object(SimpleXMLElement)#10 (2) { ["id_country"]=> string(2) "23" ["libelle"]=> string(7) "Bermuda" } [9]=> object(SimpleXMLElement)#11 (2) { ["id_country"]=> string(2) "29" ["libelle"]=> string(6) "Brazil" }}
Merci de votre aide.
eratox est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 07/02/2011, 19h37   #2
Invité de passage
 
Inscription : juin 2009
Messages : 23
Détails du profil
Informations forums :
Inscription : juin 2009
Messages : 23
Points : 2
Points : 2
Je me suis trouvé une sorte de solution :
Code :
1
2
3
 
$objet = $xml->xpath('//country[id_country="58"]') ;
echo $objet[0]->libelle;
ça vous semble propre, c'est la bonne façon de faire?
eratox est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Proposer ce sujet en actualité
Outils de la discussion



Fuseau horaire GMT +2. Il est actuellement 07h42.


 
 
 
 
Partenaires

Hébergement Web