3 /***************************************************************************\
4 * SPIP, Systeme de publication pour l'internet *
6 * Copyright (c) 2001-2019 *
7 * Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James *
9 * Ce programme est un logiciel libre distribue sous licence GNU/GPL. *
10 * Pour plus de details voir le fichier COPYING.txt ou l'aide en ligne. *
11 \***************************************************************************/
13 if (!defined('_ECRIRE_INC_VERSION')) {
19 * Transforme un texte XML en tableau PHP
21 * @param string|object $u
22 * @param bool $utiliser_namespace
25 function inc_simplexml_to_array_dist($u, $utiliser_namespace = false) {
26 // decoder la chaine en SimpleXML si pas deja fait
28 $u = simplexml_load_string($u);
31 return array('root' => @xmlObjToArr
($u, $utiliser_namespace));
36 * Transforme un objet SimpleXML en tableau PHP
37 * http://www.php.net/manual/pt_BR/book.simplexml.php#108688
38 * xaviered at gmail dot com 17-May-2012 07:00
41 * @param bool $utiliser_namespace
44 function xmlObjToArr($obj, $utiliser_namespace = false) {
48 // Cette fonction getDocNamespaces() est longue sur de gros xml. On permet donc
49 // de l'activer ou pas suivant le contenu supposé du XML
50 if (is_object($obj)) {
51 if (is_array($utiliser_namespace)) {
52 $namespace = $utiliser_namespace;
54 if ($utiliser_namespace) {
55 $namespace = $obj->getDocNamespaces(true);
57 $namespace[null] = null;
60 $name = strtolower((string)$obj->getName());
61 $text = trim((string)$obj);
62 if (strlen($text) <= 0) {
67 $attributes = array();
69 // get info for all namespaces
70 foreach ($namespace as $ns => $nsUrl) {
72 $objAttributes = $obj->attributes($ns, true);
73 foreach ($objAttributes as $attributeName => $attributeValue) {
74 $attribName = strtolower(trim((string)$attributeName));
75 $attribVal = trim((string)$attributeValue);
77 $attribName = $ns . ':' . $attribName;
79 $attributes[$attribName] = $attribVal;
83 $objChildren = $obj->children($ns, true);
84 foreach ($objChildren as $childName => $child) {
85 $childName = strtolower((string)$childName);
87 $childName = $ns . ':' . $childName;
89 $children[$childName][] = xmlObjToArr($child, $namespace);
97 $tableau['text'] = $text;
100 $tableau['attributes'] = $attributes;
103 $tableau['children'] = $children;