2010-01-21 4 views
1

j'ai simple xml ci-dessous:SimpleXML me donne les mauvais résultats

<?xml version="1.0" encoding="utf-8"?> 
<catalogue> 
    <category name="textbook" id="100" parent="books"> 
    <product id="20000"> 
     <author>Gambardella, Matthew</author> 
     <title>XML Developer's Guide</title> 
     <genre>Computer</genre> 
     <price>44.95</price> 
     <publish_date>2000-10-01</publish_date> 
     <description>An in-depth look at creating applications 
     with XML.</description> 
    </product> 
    <product id="20001"> 
     <author>Gambardellas, Matthew</author> 
     <title>XML Developer's Guide</title> 
     <genre>Computer</genre> 
     <price>44.95</price> 
     <publish_date>2000-10-01</publish_date> 
     <description>An in-depth look at creating applications 
     with XML.</description> 
    </product> 
    </category> 
    <category name="fiction" id="101" parent="books"> 
    <product id="2001"> 
     <author>Ralls, Kim</author> 
     <title>Midnight Rain</title> 
     <genre>Fantasy</genre> 
     <type>Fiction</type> 
     <price>5.95</price> 
     <publish_date>2000-12-16</publish_date> 
     <description>A former architect battles corporate zombies, an evil sorceress,     and her own childhood to become queen 
     of the world.</description> 
    </product> 
    </category> 
</catalogue> 

J'utilise la bibliothèque php SimpleXML pour analyser comme suit: (note il y a deux nœuds de catégorie La première catégorie contient deux. produits pour enfants. Mon but est d'obtenir un tableau qui contient ces deux enfants de la première « catégorie »

$xml = simplexml_load_file($xml_file) or die ("unable to load XML File!".$xml_file); 

//for each product, print out info 
$cat = array(); 
foreach($xml->category as $category) 
{ 
    if($category['id'] == 100) 
    { 
     $cat = $category;  
     break; 
    } 
} 
$prod_arr = $category->product; 

est le problème. J'attends un tableau avec deux produits enfants, mais son seul retour d'un produit. Qu'est-ce que est-ce que je fais mal ou est-ce un php bug? S'il vous plaît aider!

Répondre

2

Vous pouvez utiliser SimpleXMLElement::xpath() pour obtenir tous les éléments de produits qui sont les enfants d'un élément de catégorie spécifique. Par exemple.

// $catalogue is your $xml 
$products = $catalogue->xpath('category[@id="100"]/product'); 
foreach($products as $p) { 
    echo $p['id'], ' ', $p->title, "\n"; 
} 

impressions

20000 XML Developer's Guide 
20001 XML Developer's Guide 
1

Pour commencer, votre fichier XML n'est pas bien défini. Vous devriez probablement commencer et terminer avec la balise <categories>.

Remplacer la dernière mission avec les éléments suivants:

$prod_array = array(); 
foreach ($cat->product as $p) { 
    $prod_array[] = $p; 
} 
0
$cat = array(); 
foreach ($xml->category as $category) 
{ 
    $attributes = $category->attributes(); 
    if(isset($attributes['id']) && $attributes['id'] == 100) 
    { 
     $cat = $category; 
     break; 
    } 
} 
+0

ne jamais oublier de valider un fichier XML avant de blâmer votre code. C'est assez simple, il suffit d'ouvrir votre fichier avec firefox (ou d'essayer la commande xmllint sous Linux), il vous montrera où sont les erreurs le cas échéant. – OcuS

+0

J'ai corrigé mon code en fonction de votre montage :) – OcuS

Questions connexes