logo Debian Debian Debian-France Debian-Facile Debian-fr.org Forum-Debian.fr Debian ? Communautés logo inclusivité

Debian-facile

Bienvenue sur Debian-Facile, site d'aide pour les nouveaux utilisateurs de Debian.

Vous n'êtes pas identifié(e).

#1 14-06-2016 17:32:25

PengouinPdt
Membre
Lieu : 47 - France
Distrib. : Sid | Xebian
Noyau : Linux 5.2.xyz
(G)UI : XFCE
Inscription : 09-02-2011
Site Web

[PHP] XML vers array [Résolu]

Bonsoir,

Depuis quelques jours, je me casse les dents sur du code PHP pour extraire des données d'un fichier XML vers un tableau associatif PHP ...

Mon code PHP actuel :

private function xml_to_array($elements) {

        $array = array();

        foreach($elements as $element) {
            $name = filter_var(strip_tags($element->nodeName), FILTER_SANITIZE_STRING);

            if( $name != '#text') {
                $headline = array();

                if($element->childNodes->length != null) {

                    foreach($element->childNodes as $child) {
                        $childname = filter_var(strip_tags($child->nodeName), FILTER_SANITIZE_STRING);

                        if($child->hasChildNodes()) {

                            $this->xml_to_array($child);

                            $headline[$childname] = $child->nodeValue;

                        }
                    }

                }

                $array[$name] = $headline;
            }
        }

        unset($item); var_dump($array);

        //if(!empty($array)) { return $array; unset($array); }

    }

$elements = $dom->getElementsByTagName( 'datas' );

$version = $this->xml_to_array($elements);



Ce que je souhaite arriver à obtenir est ceci :

array(3) {
  ["zao"]=>
  array(4) {
    ["version"]=>
    string(5) "0.1.5"
    ["date"]=>
    string(35) "2015-08-26 20:15:43.912878767 +0200"
    ["name"]=>
    string(25) "Z -> Another Object (web)"
    ["url"]=>
    string(32) "https://framagit.org/hucste/ZAO/"
  }
  ["h5bp"]=>
  array(3) {
    ["name"]=>
    string(16) "HTML5BoilerPlate"
    ["url"]=>
    string(29) "https://html5boilerplate.com/"
    ["version"]=>
    string(5) "5.2.0"
  }
  ["bootstrap"]=>
  array(4) {
    ["name"]=>
    string(9) "Bootstrap"
    ["url"]=>
    string(24) "https://getbootstrap.com"
    ["version"]=>
    string(5) "3.3.6"
    ["integrity"]=>
      array(3) {
        ["css"] => string() "sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7"
        ["js"] => string() "sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS"
        ["theme"] => string() "sha384-fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r"
      }
  }
}



Bref, je n'arrive pas à boucler, ou à trouver comment boucler correctement ...
Merci d'avance pour ceux qui m'aiguilleront ! tongue

Dernière modification par PengouinPdt (16-06-2016 14:08:52)

Hors ligne

#2 14-06-2016 20:51:01

Thuban
aka prx
Distrib. : OpenBSD
Noyau : current
(G)UI : cwm
Inscription : 09-01-2009
Site Web

Re : [PHP] XML vers array [Résolu]

zut, du PHP hmm Désolé, je suis incompétent

Hors ligne

#3 15-06-2016 04:45:49

PengouinPdt
Membre
Lieu : 47 - France
Distrib. : Sid | Xebian
Noyau : Linux 5.2.xyz
(G)UI : XFCE
Inscription : 09-02-2011
Site Web

Re : [PHP] XML vers array [Résolu]

@Thuban: merci quand même ... pour l'instant, je sèche sérieux ...

Hors ligne

#4 15-06-2016 07:04:17

Thuban
aka prx
Distrib. : OpenBSD
Noyau : current
(G)UI : cwm
Inscription : 09-01-2009
Site Web

Re : [PHP] XML vers array [Résolu]

Pis le xml ça peut parfois être casse-tête à parser.
T'as demandé sur developpez.com, y aura peut-être plus de monde capable de te répondre.

Hors ligne

#5 15-06-2016 09:23:01

Anonyme-8
Invité

Re : [PHP] XML vers array [Résolu]

#6 15-06-2016 11:50:23

PengouinPdt
Membre
Lieu : 47 - France
Distrib. : Sid | Xebian
Noyau : Linux 5.2.xyz
(G)UI : XFCE
Inscription : 09-02-2011
Site Web

Re : [PHP] XML vers array [Résolu]

@niQnutn: ça ne répond pas à mon problème ...
@thuban : non, d'autant que je n'y ait pas de compte, donc pas pensé tongue

Hors ligne

#7 16-06-2016 14:07:37

PengouinPdt
Membre
Lieu : 47 - France
Distrib. : Sid | Xebian
Noyau : Linux 5.2.xyz
(G)UI : XFCE
Inscription : 09-02-2011
Site Web

Re : [PHP] XML vers array [Résolu]

J'ai enfin résolu ce problème ... cela passe par l'usage de 'SimpleXML' ... du coup, même pas besoin d'utiliser un tableau tongue
(sans parler que ça allège sérieusement mon ancien code big_smile )

Voici le code :


        $xml = simplexml_load_file($this->file);

        if( strcmp( $xml->attributes()->id, $this->action ) == 0 ) $bool = true;

        if( $bool ) {

            $this->title = $this->builderDatas( (string) $xml->title );
            $this->description = $this->builderDatas( (string) $xml->description );

            if( $this->action == 'version' ) {

                $this->version = $xml->datas;

            }
            else {

                $datas = str_replace(array('<datas>', '</datas>'), '', $xml->datas->asXML());

                $this->datas = $this->builderDatas( $datas );

                $h1 = str_replace(array('<h1>', '</h1>'), '', $xml->datas->h1->asXML());
                $this->head1 = $this->builderDatas( $h1 );
            }

        }

Hors ligne

Pied de page des forums