Parser un flux XML en PHP pour récupérer une valeur

WRInaute accro
Bonjour,

J'ai un flux XML que je veux parser en PHP. J'utilise simplexml_load_file.
Code:
<?xml version="1.0" encoding="UTF-8"?>
<catalogue id="1">
 <progname>Panzani</progname>
 <store>
  <name>Panzani Pasta</name>
  <products>
   <product>
    <storeData>
     <data type="modele">Farfale</data>
     <data type="prix_catalogue">3.35</data>
    </storeData>
  </product>
  </products>
 </store>
</catalogue>
J'arrive sans souci à récupérer la valeur de "progname" (Panzani ici), store->name mais je ne vois pas du tout comment récupérer la donnée Farfale par exemple.
J'ai testé par store->products->product->storeData->data['modele'] mais ça ne marche pas. Pourquoi ? Comment faire ?

Un grand merci
 
WRInaute accro
PHP:
<span class="syntaxhtml"><br /><span class="syntaxdefault"><?php <br />$string </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> </span><span class="syntaxkeyword"><<<EOT<br /></span><span class="syntaxdefault"><?xml version="1.0" encoding="UTF-8"?><br /><catalogue id="1"><br /><progname>Panzani</progname><br /><store><br />  <name>Panzani Pasta</name><br />  <products><br />   <product><br />    <storeData><br />     <data type="modele">Farfale</data><br />     <data type="prix_catalogue">3.35</data><br />    </storeData><br />  </product><br />  </products><br /></store><br /></catalogue><br /></span><span class="syntaxkeyword">EOT;<br /></span><span class="syntaxdefault">$xml </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> simplexml_load_string</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">$string</span><span class="syntaxkeyword">);<br /></span><span class="syntaxdefault">$data </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">(string)</span><span class="syntaxdefault"> $xml</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">store</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">products</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">product</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">storeData</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">data</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">];<br /><br /></span><span class="syntaxdefault">echo $data</span><span class="syntaxkeyword">;<br /></span><span class="syntaxdefault">?><br /></span></span>
http://codepad.org/ohKMRETR
 
WRInaute accro
Merci beaucoup, en fait par ce biais là j'y arrivais bien mais ce n'est pas très sur dans le sens où je en suis pas certain que 0 soit bien pour modèle (il suffit qu'il change de place).
J'ai donc fait autrement :
foreach($flux->store->products->product as $product){
foreach ($product->storeData->data as $node) {
$data[(string) $node['type']] = (string) $node;
}
Fourni sur un autre forum.
 
WRInaute accro
Avec SimpleXML et Xpath
PHP:
<span class="syntaxhtml"><br /><span class="syntaxdefault"><?php&nbsp;<br />$string&nbsp;</span><span class="syntaxkeyword">=&nbsp;<<<EOT<br /></span><span class="syntaxdefault"><?xml&nbsp;version="1.0"&nbsp;encoding="UTF-8"?><br /><catalogue&nbsp;id="1"><br /><progname>Panzani</progname><br /><store><br />&nbsp;&nbsp;<name>Panzani&nbsp;Pasta</name><br />&nbsp;&nbsp;<products><br />&nbsp;&nbsp;&nbsp;<product><br />&nbsp;&nbsp;&nbsp;&nbsp;<storeData><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<data&nbsp;type="modele">Farfale</data><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<data&nbsp;type="prix_catalogue">3.35</data><br />&nbsp;&nbsp;&nbsp;&nbsp;</storeData><br />&nbsp;&nbsp;</product><br />&nbsp;&nbsp;</products><br /></store><br /></catalogue><br /></span><span class="syntaxkeyword">EOT;<br /></span><span class="syntaxdefault">$xml&nbsp;</span><span class="syntaxkeyword">=&nbsp;</span><span class="syntaxdefault">simplexml_load_string</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">$string</span><span class="syntaxkeyword">);<br /></span><span class="syntaxdefault">$modele&nbsp;</span><span class="syntaxkeyword">=&nbsp;</span><span class="syntaxdefault">$xml</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">xpath</span><span class="syntaxkeyword">(</span><span class="syntaxstring">'/catalogue/store/products/product/storeData/data[@type&nbsp;=&nbsp;"modele"]'</span><span class="syntaxkeyword">);<br /></span><span class="syntaxdefault">$prix_catalogue&nbsp;</span><span class="syntaxkeyword">=&nbsp;</span><span class="syntaxdefault">$xml</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">xpath</span><span class="syntaxkeyword">(</span><span class="syntaxstring">'/catalogue/store/products/product/storeData/data[@type&nbsp;=&nbsp;"prix_catalogue"]'</span><span class="syntaxkeyword">);<br /><br /></span><span class="syntaxdefault">$modele&nbsp;</span><span class="syntaxkeyword">=&nbsp;(string)&nbsp;</span><span class="syntaxdefault">$modele</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">];<br /></span><span class="syntaxdefault">$prix_catalogue&nbsp;</span><span class="syntaxkeyword">=&nbsp;(string)&nbsp;</span><span class="syntaxdefault">$prix_catalogue</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">];<br /><br />echo&nbsp;</span><span class="syntaxdefault">$modele</span><span class="syntaxkeyword">.</span><span class="syntaxstring">'<br&nbsp;/>'</span><span class="syntaxkeyword">;<br />echo&nbsp;</span><span class="syntaxdefault">$prix_catalogue</span><span class="syntaxkeyword">;<br /></span><span class="syntaxdefault">?><br /></span></span>
 
Discussions similaires
Haut