Convertisseur de fichiers XML en Array
Classe de parsing xml permettant de convertir l'arborescence d'un fichier XML en collection (Array à plusieurs dimensions), avec SimpleXML. Idéal pour l'utilisation de fichiers de configuration en XML.
|
|
Code Source
Fonctions du code
: Crée un tableau - (PHP 4, PHP 5)
: Convertit un fichier XML en objet - (PHP 5)
: Retourne le nombre d'arguments passés à la fonction - (PHP 4, PHP 5)
: Retourne un élément de la liste des arguments - (PHP 4, PHP 5)
: Détermine si une variable est affectée - (PHP 4, PHP 5)
Commentaires
Code intéréssant mais la classe ne permet pas de l'utiliser dans le cas ou un xml contient plusieurs noeud identifique.
la méthode element devrait plutôt renvoyer un tableau contenant la liste des noeuds voulut.
la méthode element devrait plutôt renvoyer un tableau contenant la liste des noeuds voulut.
doc php
function xml2array($xml) {
if(get_class($xml) != 'SimpleXMLElement') {
if (is_file($xml)) { $xml = simplexml_load_file ($xml); }
elseif (is_string($xml)) { $xml = simplexml_load_string ($xml); }
}
if(!$xml) {
return false;
}
$main = $xml->getName();
$arr = array();
$nodes = $xml->children();
foreach($nodes as $node) {
$nodeName = $node->getName();
$nodeAttributes = $node->attributes();
$attributesArray = array();
foreach($nodeAttributes as $attributeName => $attributeValue) {
$attributesArray[$attributeName] = (string) $attributeValue;
}
$nodeValue = sizeOf($node->children()) == 0 ? trim($node) : xml2array($node);
if(!isSet($arr[$nodeName]['valeur'])) {
$arr[$nodeName]['valeur'] = $nodeValue;
$arr[$nodeName]['attributs'] = $attributesArray;
} else {
if(!is_array($arr[$nodeName]['valeur'])) {
$arr[$nodeName]['valeur'][] = array_shift($arr[$nodeName]);
$arr[$nodeName]['attributs'][] = array_shift($arr[$nodeName]['attributs']);
}
$arr[$nodeName]['valeur'][] = $nodeValue;
$arr[$nodeName]['attributs'][] = $attributesArray;
}
}
return($arr);
}
function xml2array($xml) {
if(get_class($xml) != 'SimpleXMLElement') {
if (is_file($xml)) { $xml = simplexml_load_file ($xml); }
elseif (is_string($xml)) { $xml = simplexml_load_string ($xml); }
}
if(!$xml) {
return false;
}
$main = $xml->getName();
$arr = array();
$nodes = $xml->children();
foreach($nodes as $node) {
$nodeName = $node->getName();
$nodeAttributes = $node->attributes();
$attributesArray = array();
foreach($nodeAttributes as $attributeName => $attributeValue) {
$attributesArray[$attributeName] = (string) $attributeValue;
}
$nodeValue = sizeOf($node->children()) == 0 ? trim($node) : xml2array($node);
if(!isSet($arr[$nodeName]['valeur'])) {
$arr[$nodeName]['valeur'] = $nodeValue;
$arr[$nodeName]['attributs'] = $attributesArray;
} else {
if(!is_array($arr[$nodeName]['valeur'])) {
$arr[$nodeName]['valeur'][] = array_shift($arr[$nodeName]);
$arr[$nodeName]['attributs'][] = array_shift($arr[$nodeName]['attributs']);
}
$arr[$nodeName]['valeur'][] = $nodeValue;
$arr[$nodeName]['attributs'][] = $attributesArray;
}
}
return($arr);
}
Ajouter un commentaire
Librairie PHP
Connexion
XML
PHP
- Affichage (5)
- Applications (9)
- Chaînes (71)
- Classes (29)
- Constantes (4)
- Cookies (3)
- Date-Heure (40)
- Email (14)
- Fichiers - Réps (45)
- Fonctions (23)
- Formulaires (18)
- GD-Graphiques (11)
- Google (13)
- HTML (7)
- Images (21)
- Inclassable (28)
- Maths (24)
- MySQL (23)
- Navigateurs (6)
- Recherches (6)
- Regex (13)
- Reseau (2)
- Sécurité (25)
- Sessions (13)
- Statistiques (5)
- Système (18)
- Tableaux (21)
- Tag-cloud (3)
- Templates (3)
- Twitter (5)
- URL (22)
- Variables (8)
- Vidéos (2)
- Windows (1)
- XML (2)
|
||























