Bonjour,
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 ...
Ce que je souhaite arriver à obtenir est ceci :
Mon code PHP actuel :
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 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" } }
La méthode 'xml_to_array()' actuelle est ainsi :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11 if( $this->action == 'version' ) { $elements = $dom->getElementsByTagName( 'datas' ); $version = $this->xml_to_array($elements); if(!empty($version)) $this->version = $version; unset($elements, $version); }
Bref, dans ce contexte, j'ai essayé de modifier le code de la méthode 'xml_to_array' pour qu'elle soit plus "dynamique" et puisse parcourir l'arbre xml de mon fichier ... mais quelque soit les essais, je n'arrive pas à descendre au niveau des noeuds enfants du noeud integrity ...
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 private function xml_to_array($elements) { $array = array(); foreach($elements as $element) { if($element->childNodes->length != null) { foreach($element->childNodes as $idx) { $idx_name = filter_var(strip_tags($idx->nodeName), FILTER_SANITIZE_STRING); if( $idx_name != '#text') { $array[$idx_name] = array(); if($idx_name == "zao" && $idx->getAttribute('version')) $array[$idx_name]['version'] = $idx->getAttribute('version'); if($idx->childNodes->length != null) { foreach($idx->childNodes as $info) { if($info->nodeName != '#text') { $info_name = filter_var(strip_tags($info->nodeName), FILTER_SANITIZE_STRING); $info_value = filter_var(strip_tags($info->nodeValue), FILTER_SANITIZE_STRING); $array[$idx_name][$info_name] = $info_value; unset($info_name, $info_value); } } unset($info); } } unset($idx_name); } unset($idx); } } unset($element); if(!empty($array)) { return $array; unset($array); } }
Il y a quelque chose que je ne suis pas arrivé à comprendre ...
Merci d'avance pour l'aide
Partager