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 :

[XML] XML et PHP : la galère !


Sujet :

Bibliothèques et frameworks PHP

  1. #1
    Membre averti
    Inscrit en
    Mars 2009
    Messages
    44
    Détails du profil
    Informations forums :
    Inscription : Mars 2009
    Messages : 44
    Par défaut [XML] XML et PHP : la galère !
    Bojour, je reçois un fichier XMl de ce style (sans les traits d'unions en début de certaines lignes, c'est le copier/coller d'internet explorer çà) :

    Code : 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
    <?xml version="1.0" encoding="UTF-8" ?> 
      <!DOCTYPE ICECAT-interface (View Source for full doctype...)> 
    - <!--  source: Icecat.biz 2011 
      --> 
    - <ICECAT-interface xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://data.icecat.biz/xsd/ICECAT-interface_response.xsd">
    - <Product Code="1" HighPic="http://images.icecat.biz/img/norm/high/1658576-HP.jpg" HighPicHeight="400" HighPicSize="28984" HighPicWidth="400" ID="1652451" LowPic="http://images.icecat.biz/img/norm/low/1658576-HP.jpg" LowPicHeight="200" LowPicSize="17101" LowPicWidth="200" Name="Imprimante HP Color LaserJet CP2025n" Prod_id="CB494A#B19" Quality="ICECAT" ReleaseDate="2008-08-03" ThumbPic="http://images.icecat.biz/thumbs/1658576.jpg" ThumbPicSize="4544" Title="HP Gamme d'imprimantes HP Color LaserJet CP2020 Imprimante HP Color LaserJet CP2025n">
    - <Category ID="235">
      <Name ID="343980" Value="imprimantes laser et LED" langid="3" /> 
      </Category>
    - <CategoryFeatureGroup ID="353" No="-1">
    - <FeatureGroup ID="0">
      <Name ID="344646" Value="Détails techniques" langid="3" /> 
      </FeatureGroup>
      </CategoryFeatureGroup>
    - <CategoryFeatureGroup ID="906" No="39">
    - <FeatureGroup ID="1">
      <Name ID="344619" Value="Processeur" langid="3" /> 
      </FeatureGroup>
      </CategoryFeatureGroup>
    - <CategoryFeatureGroup ID="2747" No="0">
    - <FeatureGroup ID="2">
      <Name ID="344620" Value="Lecteur de disque" langid="3" /> 
      </FeatureGroup>
      </CategoryFeatureGroup>
    <EANCode EAN="8835856138612" /> 
      <EANCode EAN="0883585613861" /> 
      <ProductBundled /> 
      <ProductDescription ID="22015773" LongDesc="" ManualPDFSize="0" ManualPDFURL="" PDFSize="0" PDFURL="" ShortDesc="HP Color LaserJet CP2025n Printer" URL="" WarrantyInfo="Garantie: Garantie limitée d'un an, retour au centre de service agréé. La garantie et les options d'assistance varient selon le produit, le pays et les spécifications juridiques locales.\n" langid="3" /> 
    - <ProductFamily ID="48773">
      <Name ID="647790" Value="Gamme d'imprimantes HP Color LaserJet CP2020" langid="3" /> 
    - <Series ID="1">
      <Name /> 
      </Series>
      </ProductFamily>
    - <ProductFeature Localized="1" ID="35736970" Local_ID="19130392" Value="Y" CategoryFeature_ID="1291" CategoryFeatureGroup_ID="80" No="10114026" Presentation_Value="Oui" Translated="1" Mandatory="1" Searchable="1">
    - <Feature ID="786">
    - <Measure ID="29" Sign="">
      <Signs /> 
      </Measure>
      <Name ID="626097" langid="3" Value="Réseau prêt" /> 
      </Feature>
      </ProductFeature>
    - <ProductFeature Localized="0" ID="45962001" Local_ID="0" Value="Y" CategoryFeature_ID="77" CategoryFeatureGroup_ID="80" No="10106624" Presentation_Value="Oui" Translated="1" Mandatory="1" Searchable="1">
    - <Feature ID="46">
    - <Measure ID="29" Sign="">
      <Signs /> 
      </Measure>
      <Name ID="344613" langid="3" Value="Couleur" /> 
      </Feature>
      </ProductFeature>
    <ProductMultimediaObject>
      <MultimediaObject ContentType="text/html" Date="2011-09-06 10:19:13" Description="HP Color LaserJet CP2025n Printer" Height="0" KeepAsURL="1" MultimediaObject_ID="30740" Size="0" Type="movie" URL="http://objects.icecat.biz/objects/1652451-30740.html" Width="0" langid="3" /> 
      <MultimediaObject ContentType="text/html" Date="2010-01-05 13:43:42" Description="Imprimante HP Color LaserJet série CP2020" Height="0" KeepAsURL="1" MultimediaObject_ID="201314" Size="425895" Type="data sheet" URL="http://objects.icecat.biz/objects/1652451-201314.html" Width="0" langid="3" /> 
      </ProductMultimediaObject>
    <SummaryDescription>
      <ShortSummaryDescription langid="3">HP Imprimante HP Color LaserJet CP2025n Gamme d'imprimantes HP Color LaserJet CP2020, Laser, 600 x 600 DPI, 750 à 2000 pages, 20 ppm, 20 ppm, 17.8 s</ShortSummaryDescription> 
      <LongSummaryDescription langid="3">HP Imprimante HP Color LaserJet CP2025n, Gamme d'imprimantes HP Color LaserJet CP2020. Qualité d'impression: Laser, 600 x 600 DPI, 750 à 2000 pages. Vitesse d'impression: 20 ppm, 20 ppm, 17.8 s, < 18 secondes (en mode Prêt). Mémoire vive: 128 Mo, 1 144-pin, DDR2 DIMM slot, DDR2, 384 Mo, Aucun. Processeur: 540 MHz, Motorola Coldfire V5x. Capacité d'entrée: 300 feuilles, feuilles : 50, transparents : 50, enveloppes : 10 feuilles. Capacité de sortie: 150 feuilles, 150 feuilles. Gestion du papier: Papier ordinaire, Bond paper, Papier recyclé, Enveloppes, Étiquettes, Transparences, Papier glacé, A4, A5, A6, B5, ISO DL, ISO C5, ISO B5. Connectivité: None. Technologie d'impression: HP PCL 6, HP PCL 5c, HP Postscript level 3 emulation, CMYB 4 . Gestion d'énergie: 445 watts, 6,7 watts, 0,48 watt. Poids et dimensions: 405 x 454 x 322 mm, 22700 g. Emballage: 26400 g, 600 x 500 x 479 mm. Conditions environnementales: 10 - 80 %, -20 - 40 °C, 15 - 30 °C, 10 - 95 %, 9144 m, 59 - 80.6 °F. Poids des supports: 60 to 176 g/m² (up to 220 g/m² with HP Color Laser Glossy Photo papers)</LongSummaryDescription> 
      </SummaryDescription>
      <Supplier ID="1" Name="HP" /> 
      </Product>
      </ICECAT-interface>
    Et j'aimerais pouvoir en PHP récupérer ce qui est en rouge par exemple

    J'ai comme base ce code :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    $rss_file = file_get_contents("product.xml"); 
    $xml = new SimpleXMLElement($rss_file); 
    foreach($xml->Product[0]->attributes() as $a => $b) {    
    	echo "<li>".$a, '="', $b, "\"";
    }
    Ou celui-ci

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
      	$dom = new DomDocument();
      	$dom->load("product.xml");
     	$products = $dom->getElementsByTagName("Product");
    	$count_elements = $products->length; 
    	//Si $product->getAttribute("ID")=="" c'est que IP non autorisée dans WWW.ICECAT.BIZ
    	if ($count_elements>0) {;
    		foreach ($products as $product) {
    ...
    C'est mieux DoDoculment ou SimpleXMLElement ?

  2. #2
    Expert confirmé
    Avatar de Séb.
    Profil pro
    Inscrit en
    Mars 2005
    Messages
    5 344
    Détails du profil
    Informations personnelles :
    Âge : 47
    Localisation : France

    Informations professionnelles :
    Secteur : High Tech - Opérateur de télécommunications

    Informations forums :
    Inscription : Mars 2005
    Messages : 5 344
    Billets dans le blog
    17
    Par défaut
    SimpleXML me semble plus adapté à la lecture d'un petit XML.

    PS : tu peux faire simplexml_load_file( ) au lieu de file_get_contents( ) + new SimpleXMLElement( )

  3. #3
    Membre averti
    Inscrit en
    Mars 2009
    Messages
    44
    Détails du profil
    Informations forums :
    Inscription : Mars 2009
    Messages : 44
    Par défaut
    OK, admettons que j'utilise simplexml_load_file

    Comment je fais pour aller lire ce qui est en rouge ?

    Et surtout parcourir en boucle l'arborescence qui contient plusieurs CategoryFeatureGroupe, etc ?

  4. #4
    Expert confirmé
    Avatar de Séb.
    Profil pro
    Inscrit en
    Mars 2005
    Messages
    5 344
    Détails du profil
    Informations personnelles :
    Âge : 47
    Localisation : France

    Informations professionnelles :
    Secteur : High Tech - Opérateur de télécommunications

    Informations forums :
    Inscription : Mars 2005
    Messages : 5 344
    Billets dans le blog
    17
    Par défaut
    Comment je fais pour aller lire ce qui est en rouge ?

    Et surtout parcourir en boucle l'arborescence qui contient plusieurs CategoryFeatureGroupe, etc ?
    Tu as des exemples ici : http://fr.php.net/manual/en/simplexm...ples-basic.php
    Et tu peux interroger le XML avec XPath : http://fr.php.net/manual/en/simplexmlelement.xpath.php

  5. #5
    Membre averti
    Inscrit en
    Mars 2009
    Messages
    44
    Détails du profil
    Informations forums :
    Inscription : Mars 2009
    Messages : 44
    Par défaut
    Je suis le premier manuel fourni, mais je n'y arrive pas

    Je voudrais par exemple afficher toutes les valeurs (champs VALUE sous Name), j'essaye ceci :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
     
    $rss_file = file_get_contents("http://user:password@data.icecat.biz/xml_s3/xml_server3.cgi?ean_upc=".$EAN.";lang=fr;output=productxml"); 
    $Product = new SimpleXMLElement($rss_file); 
     
    foreach ($Product->CategoryFeatureGroup->FeatureGroup->Name  as $name) {
       echo $name['Value'];
    }
    J'ai aussi essayé via simplexml_load_file :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     
    $Product = simplexml_load_file("http://user:password@data.icecat.biz/xml_s3/xml_server3.cgi?ean_upc=".$EAN.";lang=fr;output=productxml");     
     
    var_dump($Product);
    echo "<hr>".Product; 
    foreach ($Product as $Dtl -> $Value) {
    	echo $Dtl["Code"];
    }

  6. #6
    Expert confirmé

    Profil pro
    Inscrit en
    Septembre 2010
    Messages
    7 920
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2010
    Messages : 7 920

  7. #7
    Membre averti
    Inscrit en
    Mars 2009
    Messages
    44
    Détails du profil
    Informations forums :
    Inscription : Mars 2009
    Messages : 44
    Par défaut
    Je viens d'essayer

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    foreach($Product->attributes() as $a => $b) {
        echo $a,'="',$b,"\"\n";
    }
    et çà ne retourn rien

  8. #8
    Expert confirmé

    Profil pro
    Inscrit en
    Septembre 2010
    Messages
    7 920
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2010
    Messages : 7 920
    Par défaut
    faut choisir ton noeud la dans protuct ta que le root ...
    vérifie au moins tes valeurs avec des var_dump de temps en tant, ca sert a rien de tâtonner a l'aveugle

  9. #9
    Membre averti
    Inscrit en
    Mars 2009
    Messages
    44
    Détails du profil
    Informations forums :
    Inscription : Mars 2009
    Messages : 44
    Par défaut
    Je suis bien d'accord avec toi, je suis sur le Root, mais je veux en récupérer les valeurs telles que celles du "Code", de "HighPic", ...

    Ensuite il faudra que je parcours en boucle pour lire les noeuds.
    Je tâtonne parceque je ne connais pas du tout XML ! Sinon je ne saurais pas ici à vous ennuyer

  10. #10
    Expert confirmé

    Profil pro
    Inscrit en
    Septembre 2010
    Messages
    7 920
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2010
    Messages : 7 920
    Par défaut
    fait un var_dump et parcours ton arbre comme n'importe quelle array/object dans PHP, y'a rien de spécial ici

  11. #11
    Membre averti
    Inscrit en
    Mars 2009
    Messages
    44
    Détails du profil
    Informations forums :
    Inscription : Mars 2009
    Messages : 44
    Par défaut
    Je ne m'en sort pas mieux tellement la structure est complexe

  12. #12
    Expert confirmé

    Profil pro
    Inscrit en
    Septembre 2010
    Messages
    7 920
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2010
    Messages : 7 920
    Par défaut
    tu l’avais déjà :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    $Product->CategoryFeatureGroup->FeatureGroup->Name

  13. #13
    Membre averti
    Inscrit en
    Mars 2009
    Messages
    44
    Détails du profil
    Informations forums :
    Inscription : Mars 2009
    Messages : 44
    Par défaut
    Au cas où il y aurait des nulls comme moi qui lisent ce post, voilà comment je m'en sors :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    $Product = simplexml_load_file("http://username:password@data.icecat.biz/xml_s3/xml_server3.cgi?ean_upc=".$EAN.";lang=fr;output=productxml");     
     
    $product_code                = $Product->Product[0]["Code"];
    $product_category            = $Product->Product[0]->Category[0]["ID"];
    $product_category_name_id    = $Product->Product[0]->Category[0]->Name[0]["ID"];
    $product_category_name_value = $Product->Product[0]->Category[0]->Name[0]["Value"];
     
    for($i=0;$i < sizeof($Product->Product[0]->CategoryFeatureGroup);$i++) 
    { 
       echo '<li>'.$Product->Product[0]->CategoryFeatureGroup[$i]->FeatureGroup[0]->Name[0]["ID"]; 
    }

  14. #14
    Expert confirmé

    Profil pro
    Inscrit en
    Septembre 2010
    Messages
    7 920
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2010
    Messages : 7 920
    Par défaut
    montre ton XML (complet)

Discussions similaires

  1. [DOM XML] XML -> tableau PHP
    Par Mucsy dans le forum Bibliothèques et frameworks
    Réponses: 6
    Dernier message: 03/06/2005, 11h44
  2. [XSLT] transformation XML->XML en php
    Par amellouki dans le forum XSL/XSLT/XPATH
    Réponses: 5
    Dernier message: 03/03/2005, 12h32
  3. [XSLT] XML et PHP
    Par Dam'$ dans le forum Bibliothèques et frameworks
    Réponses: 10
    Dernier message: 05/08/2004, 15h14
  4. [XML et PHP]
    Par Mister Nono dans le forum XML/XSL et SOAP
    Réponses: 4
    Dernier message: 22/03/2004, 13h57
  5. XML + XSL + PHP
    Par ZzZerf dans le forum XSL/XSLT/XPATH
    Réponses: 5
    Dernier message: 21/03/2003, 15h54

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