IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Bibliothèques et frameworks PHP Discussion :

[SimpleXML] Analyser un fichier XML


Sujet :

Bibliothèques et frameworks PHP

  1. #1
    Membre averti
    Homme Profil pro
    Ingénieur sécurité
    Inscrit en
    Avril 2007
    Messages
    234
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 39
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Ingénieur sécurité

    Informations forums :
    Inscription : Avril 2007
    Messages : 234
    Points : 338
    Points
    338
    Par défaut [SimpleXML] Analyser un fichier XML
    Bonjour
    J'essaye de parser un XML mais je n'arrive pas à récupérer certaines informations dans mon xML:
    D'abord voici à quoi ressemble mon XML:
    Code XML : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <product upc="00606949350727">
    	          ...
    	<prd_explicit_lyrics>N</prd_explicit_lyrics>
    	<p_line>(P) 2002 Interscope Geffen (A&amp;M) Records A Division of UMG Recordings Inc.</p_line>
    	<c_line>(C) 2002 Geffen Records Inc.</c_line>
    	<prd_pricing>
    		<current_price_code>MP1</current_price_code>
    		<current_price_effective_date>04-Jan-2007</current_price_effective_date>
    		<current_campaign campaign_id="I702445">Universal Top 100 Artists</current_campaign>
    		<current_campaign_end_date>31-Jan-2007</current_campaign_end_date>
    		<current_campaign_end_price_code>TP1</current_campaign_end_price_code>
    		<new_price_code>priceTOTO</new_price_code>
    		<new_price_effective_date></new_price_effective_date>
    	</prd_pricing>
    	<prd_pricing>
    		<current_price_code>MP2</current_price_code>
    		<current_price_effective_date>06-Fev-2008</current_price_effective_date>
    		<current_campaign campaign_id="I702445">Universal Top 100 Artists</current_campaign>
    		<current_campaign_end_date>28-Fev-2008</current_campaign_end_date>
    		<current_campaign_end_price_code>TP2</current_campaign_end_price_code>
    		<new_price_code>priceTOTO2</new_price_code>
    		<new_price_effective_date></new_price_effective_date>
    	</prd_pricing>
    	<prd_contributors>
    		<artist_name>Nirvana</artist_name>
    		</prd_contributors>
    	<terms>
    		<note></note>
    	</terms>
    	<prd_associated_products/>
    	<tracks>
    		<track isrc="USIR10211639">
    			<type>new</type>
    			<volume>1</volume>
    			<track_number>1</track_number>
    			<track_title>You Know You're Right</track_title>
    			<track_version_title>Album Version</track_version_title>
    			<track_hierarchy_title></track_hierarchy_title>
    			<asset_type>AUDIO</asset_type>
    			<track_length>0:03:38</track_length>
    			<track_label>Geffen</track_label>
    			<track_explicit_lyrics></track_explicit_lyrics>
    			<track_genre>Rock</track_genre>
    			<track_e_marketing_tag></track_e_marketing_tag>
    			<track_owning_label></track_owning_label>
    			<p_line>(P) 2002 Interscope Geffen (A&amp;M) Records A Division of UMG Recordings Inc.</p_line>
    			<c_line></c_line>
    			<track_pricing>
    				<current_price_code>Unpriced</current_price_code>
    				<current_price_effective_date>04-Jan-2007</current_price_effective_date>
    				<new_price_code></new_price_code>
    				<new_price_effective_date></new_price_effective_date>
    			</track_pricing>
    			<track_pricing>
    				<current_price_code>TOTO Unpriced</current_price_code>
    				<current_price_effective_date>toto 04-Jan-2007</current_price_effective_date>
    				<new_price_code></new_price_code>
    				<new_price_effective_date></new_price_effective_date>
    			</track_pricing>
    			<track_contributors>
    				<artist_name>Nirvana</artist_name>
    				<composer>Kurt Cobain</composer>
    			</track_contributors>
    			<work_level>
    				<work_id></work_id>
    				<work_title></work_title>
    				<parent_work_id></parent_work_id>
    				<parent_work_title></parent_work_title>
    			</work_level>
    			<terms>
    				<singles_restriction>N</singles_restriction>
    				<master_use>
    					<mu_permanent>Y</mu_permanent>
    					<mu_streaming>Y</mu_streaming>
    					<mu_burns>Y</mu_burns>
    					<mu_transfers>Y</mu_transfers>
    					<mu_limited>Y</mu_limited>
    				</master_use>
    				<distribution_use>
    					<channel name="Audio Download">Y</channel>
    					<channel name="Audio Subscription">N</channel>
    				</distribution_use>
    				<note></note>
    			</terms>
    			<associated_products/>
    			<associated_tracks/>
    		</track>
    		<track isrc="TOTO10211639">
    			<type>new</type>
    			<volume>1</volume>
    			<track_number>1</track_number>
    			<track_title>You Know You're Righttoto</track_title>
    			<track_version_title>Album Version</track_version_title>
    			<track_hierarchy_title></track_hierarchy_title>
    			<asset_type>AUDIO</asset_type>
    			<track_length>0:03:38</track_length>
    			<track_label>Geffen</track_label>
    			<track_explicit_lyrics></track_explicit_lyrics>
    			<track_genre>Rock</track_genre>
    			<track_e_marketing_tag></track_e_marketing_tag>
    			<track_owning_label></track_owning_label>
    			<p_line>(P) 2002 Interscope Geffen (A&amp;M) Records A Division of UMG Recordings Inc.</p_line>
    			<c_line></c_line>
    			<track_pricing>
    				<current_price_code>Unpriced3</current_price_code>
    				<current_price_effective_date>04-Jan-2007</current_price_effective_date>
    				<new_price_code></new_price_code>
    				<new_price_effective_date></new_price_effective_date>
    			</track_pricing>
    			<track_contributors>
    				<artist_name>Nirvana</artist_name>
    				<composer>Kurt Cobain</composer>
    			</track_contributors>
    			<work_level>
    				<work_id></work_id>
    				<work_title></work_title>
    				<parent_work_id></parent_work_id>
    				<parent_work_title></parent_work_title>
    			</work_level>
    			<terms>
    				<singles_restriction>N</singles_restriction>
    				<master_use>
    					<mu_permanent>Y</mu_permanent>
    					<mu_streaming>Y</mu_streaming>
    					<mu_burns>Y</mu_burns>
    					<mu_transfers>Y</mu_transfers>
    					<mu_limited>Y</mu_limited>
    				</master_use>
    				<distribution_use>
    					<channel name="Audio Download">Y</channel>
    					<channel name="Audio Subscription">N</channel>
    				</distribution_use>
    				<note></note>
    			</terms>
    			<associated_products/>
    			<associated_tracks/>
    		</track>
    	</tracks>
    </product>

    Il y a donc une balise tracks qui contient 2 balises track. Dans ces balises tracks il peut y avoir des balises track_pricing (un nombre aléatoire pour chaque balises track).
    J'aimerai pouvoir récupérer, pour charque balise track, les track_pricing qui y sont associés.

    J'ai essayé de faire:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    <?php
    $chemin = 'files/'.$fichier;
    $xml = new SimpleXMLElement(file_get_contents($chemin));
    $tab = $xml->xpath('tracks/track/track_pricing');
    debug($tab);
    ?>
    Le problème est que le tableau $tab contient tous les track_pricing du document XML sans tenir compte du track auxquels ils appartiennent.

    Quelqu'un saurait comment faire svp?

    Merci

  2. #2
    Expert éminent sénior

    Profil pro
    Inscrit en
    Juin 2002
    Messages
    6 152
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2002
    Messages : 6 152
    Points : 17 777
    Points
    17 777
    Par défaut
    N'est-il pas possible de faire cela avec des boucles foreach :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    $chemin = 'files/'.$fichier;
    $sxml = new SimpleXMLElement(file_get_contents($chemin));
    foreach ($sxml->tracks->track as $t) {
        echo 'Titre : ' . $t->track_title; // Exemple
        foreach ($t->track_pricing as $tp) {
            echo 'Code prix : ' . $tp->current_price_code; // Exemple
        }
    }
    (non testé)

  3. #3
    Membre averti
    Homme Profil pro
    Ingénieur sécurité
    Inscrit en
    Avril 2007
    Messages
    234
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 39
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Ingénieur sécurité

    Informations forums :
    Inscription : Avril 2007
    Messages : 234
    Points : 338
    Points
    338
    Par défaut
    Merci ca marche tres bien comme solution

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [SimpleXML] Traitement de fichier XML
    Par RasDataMan dans le forum Bibliothèques et frameworks
    Réponses: 1
    Dernier message: 26/11/2007, 11h44
  2. [XML] [EXPAT] Analyser un fichier XML
    Par namstou3 dans le forum Bibliothèques et frameworks
    Réponses: 1
    Dernier message: 07/11/2007, 18h50
  3. [SimpleXML] Analyser un fichier XML avec PHP
    Par Someone86 dans le forum Bibliothèques et frameworks
    Réponses: 1
    Dernier message: 10/08/2007, 09h43
  4. [XML] Analyser un fichier XML en PHP
    Par aroua dans le forum Bibliothèques et frameworks
    Réponses: 2
    Dernier message: 06/07/2007, 11h07
  5. [SimpleXML] Lire un fichier XML
    Par ChriGoLioNaDor dans le forum Bibliothèques et frameworks
    Réponses: 4
    Dernier message: 07/02/2007, 16h48

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo