Débutant : SimpleXML avec un namespace

Eléphanteau du PHP | 17 Messages

14 mai 2011, 16:57

Bonjour à tous,

Le XML n'étant plus tout jeune, il y a de nombreuse façon de récupérer des données pour l'exploiter ailleurs.
Après avoir perdu mon temps avec le XSL je m'aperçois que SimpleXML m'est plus utile, encore faut-il l'apprivoiser !

Voici ma question !
J'ai un fichier xml avec un namespace qui me complique la vie voir ci dessous (tif : tourinfrance) :

Code : Tout sélectionner

<?xml version="1.0" encoding="utf-8"?> <OIS> <OI xmlns:tif="http://www.tourinfrance.net/Tourinfrance3/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ms="http://microsoft.com/wsdl/types/"> <!--Granule DublinCore--> <!--Granule Contacts--> <tif:Contacts> <tif:DetailContact type="04.03.13" xml:lang="fr" libelle="Etab/Lieu/Structure"> <tif:RaisonSociale> CAMPING DE LA MER </tif:RaisonSociale> <tif:Sigle /> <tif:Adresses> <tif:DetailAdresse> <tif:Adr1> La Vaillaisse </tif:Adr1> <tif:Adr2> 90 route de XXXXX </tif:Adr2> <tif:Adr3 /> <tif:CodePostal> 55555 </tif:CodePostal> <tif:Commune code="56214"> ST TRUC DES CHOSES </tif:Commune> <tif:Cedex /> <tif:Personnes> <tif:DetailPersonne id="PRO1_56542" type="04.04.04" xml:lang="fr" libelle="Indéterminé"> <tif:MoyensCommunications> <tif:DetailMoyenCom type="04.02.05" xml:lang="fr" libelle="Site web (URL)"> <tif:Coord> http://www.site.com </tif:Coord> <tif:ObservationDetailMoyenCom /> </tif:DetailMoyenCom> <tif:DetailMoyenCom type="04.02.02" xml:lang="fr" libelle="Télécopieur /fax"> <tif:Coord> 02 22 22 22 22 </tif:Coord> <tif:ObservationDetailMoyenCom /> </tif:DetailMoyenCom> <tif:DetailMoyenCom type="04.02.04" xml:lang="fr" libelle="Email"> <tif:Coord> [email protected] </tif:Coord> <tif:ObservationDetailMoyenCom /> </tif:DetailMoyenCom> <tif:DetailMoyenCom type="04.02.01" xml:lang="fr" libelle="Téléphone"> <tif:Coord> 02 00 00 00 00 </tif:Coord> <tif:ObservationDetailMoyenCom /> </tif:DetailMoyenCom> <tif:DetailMoyenCom type="04.02.50" xml:lang="fr" libelle="Téléphone mobile"> <tif:Coord> +33 00 00 00 00 00 </tif:Coord> <tif:ObservationDetailMoyenCom /> </tif:DetailMoyenCom> </tif:MoyensCommunications> </tif:DetailPersonne> </tif:Personnes> </tif:DetailAdresse> </tif:Adresses> </tif:DetailContact> </tif:Contacts> </OI> </OIS>
La question est simple : comment récupérer, le nom de la strucutre (Camping de la mer), le fax (02 22 22 22 22), la valeur Adr2...
En fait comment se dépatouiller avec ce flux ? Les exemples que je trouve sur internet dans les tutoriaux sont très simplistes du genre :
$xml = simplexml_load_file('exemple.xml');
print $xml->OIS->OI->tif:contacts
Avez-vous quelque chose d'aussi simple mais qui marche avec cette structure particulière au namespace ?

Merci :)

ViPHP
ViPHP | 5462 Messages

16 mai 2011, 14:00

tu peu utilser xpath mais il faut enregistrer les namespaces avec registerXPathNamespace
http://www.php.net/manual/fr/simplexmle ... espace.php

Eléphanteau du PHP | 17 Messages

17 mai 2011, 15:34

Souper !! Merci pour le tuyau !

J'en profite pour glisser une question subsidiaire (relou) !
Pour récupérer les données de la branche ci-dessous avec un attribut :
<tif:DetailContact type="04.03.30" libelle="Propriétaire">
$fichier = file_get_contents('test.xml');
$xml = new SimpleXMLElement($fichier);

$xml->registerXPathNamespace('c', 'http://www.tourinfrance.net/Tourinfrance3/');

$result = $xml->xpath('//c:RaisonSociale');
foreach ($result as $title) {
  echo $title . "\n";
}
Je précise que ce n'est pas la valeur de l'attribut que je souhaite mais la valeur de <tif:DetailContact> qui a l'attribut "libelle=proprietaire" pour la différencier de celle qui a l'attribut "libelle=trucbidule".

Merci énormément !