Probleme Parsing FICHIER XML avec Attributs

Petit nouveau ! | 2 Messages

27 févr. 2009, 15:45

voici un exemple de XML avec attributs:

<DOSSIER>
<DOSSIER_ENTREES>
<A1 v="zzz" />
<B1 v=" il pleuvait fort sur la grand route" />
<C1 v="FFFDGFDFDG" />
<D1 v="16/10/2008" />
</DOSSIER_ENTREES>
<SITUATION>
<A1 v="s4-1" />
<B1 v="ceci est un descriptif de test" />
<C1 v="2000" />
</SITUATION>
</DOSSIER>

comment faire pour recuper A1,B1,C1 je ne m'en sors pas
merci d'avance
@+
JP

Mammouth du PHP | 2937 Messages

27 févr. 2009, 22:24

Avec SimpleXML, on peut s'en sortir. ;)
$fichier = 'chemin-vers-ton-fichier.xml';
// On charge le fichier
$xml = simplexml_load_file ($fichier);

// Récupérer la valeur de l'attribut du premier A1
/* Il suffit de parcourir l'arboresence du XML,
la variable $xml pointant l'élément racine */
$a1 = $xml -> DOSSIER_ENTREES[0] -> A1['v'];
/* L'index [0] est utile pour le cas où il y aurait plusieurs éléments DOSSIER_ENTREES
enfants de l'élément racine */

// Afficher la valeur récupérée en forçant le typage
echo (string) $a1; // Affiche zzz
Si le XML est contenu dans une chaîne, il faut alors remplacer simplexml_load_file par simplexml_load_string.

Petit nouveau ! | 2 Messages

02 mars 2009, 10:11

Salut

Merci de votre reponse si rapide

@+

JP

Petit nouveau ! | 1 Messages

05 mars 2009, 13:19

Bonjour,

j'ai un pb un peu similaire.
Comment récupérer TIME_PERIOD et OBS_VALUE ?
Sachant que j'utilise simplexml_load_file.
Merci.

dans le XML suivant :

Code : Tout sélectionner

<DataSet xmlns="http://www.ecb.int/vocabulary/stats/exr/1" xsi:schemaLocation="http://www.ecb.int/vocabulary/stats/exr/1 https://stats.ecb.int/stats/vocabulary/exr/1/2006-09-04/sdmx-compact.xsd"> <Group CURRENCY="USD" CURRENCY_DENOM="EUR" EXR_TYPE="SP00" EXR_SUFFIX="A" DECIMALS="4" UNIT="USD" UNIT_MULT="0" TITLE_COMPL="ECB reference exchange rate, US dollar/Euro, 2:15 pm (C.E.T.)" /> <Series FREQ="D" CURRENCY="USD" CURRENCY_DENOM="EUR" EXR_TYPE="SP00" EXR_SUFFIX="A" TIME_FORMAT="P1D" COLLECTION="A"> <Obs TIME_PERIOD="1999-01-04" OBS_VALUE="1.1789" OBS_STATUS="A" OBS_CONF="F" /> <Obs TIME_PERIOD="1999-01-05" OBS_VALUE="1.1790" OBS_STATUS="A" OBS_CONF="F" /> <Obs TIME_PERIOD="1999-01-06" OBS_VALUE="1.1743" OBS_STATUS="A" OBS_CONF="F" /> <Obs TIME_PERIOD="1999-01-07" OBS_VALUE="1.1632" OBS_STATUS="A" OBS_CONF="F" /> .... <Obs TIME_PERIOD="2009-03-02" OBS_VALUE="1.2596" OBS_STATUS="A" OBS_CONF="F" /> <Obs TIME_PERIOD="2009-03-03" OBS_VALUE="1.2615" OBS_STATUS="A" OBS_CONF="F" /> </Series></DataSet>