3 /***************************************************************************\
4 * SPIP, Systeme de publication pour l'internet *
6 * Copyright (c) 2001-2016 *
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')) return;
19 * Transforme un texte XML en tableau PHP
20 * @param string|object $u
21 * @param bool $utiliser_namespace
24 function inc_simplexml_to_array_dist($u, $utiliser_namespace=false){
25 // decoder la chaine en SimpleXML si pas deja fait
27 $u = simplexml_load_string($u);
28 return array('root'=>@xmlObjToArr
($u, $utiliser_namespace));
33 * Transforme un objet SimpleXML en tableau PHP
34 * http://www.php.net/manual/pt_BR/book.simplexml.php#108688
35 * xaviered at gmail dot com 17-May-2012 07:00
38 * @param bool $utiliser_namespace
41 function xmlObjToArr($obj, $utiliser_namespace=false) {
45 // Cette fonction getDocNamespaces() est longue sur de gros xml. On permet donc
46 // de l'activer ou pas suivant le contenu supposé du XML
47 if (is_object($obj)) {
48 if (is_array($utiliser_namespace)){
49 $namespace = $utiliser_namespace;
52 if ($utiliser_namespace)
53 $namespace = $obj->getDocNamespaces(true);
54 $namespace[NULL] = NULL;
57 $name = strtolower((string)$obj->getName());
58 $text = trim((string)$obj);
59 if (strlen($text) <= 0) {
64 $attributes = array();
66 // get info for all namespaces
67 foreach( $namespace as $ns=>$nsUrl ) {
69 $objAttributes = $obj->attributes($ns, true);
70 foreach( $objAttributes as $attributeName => $attributeValue ) {
71 $attribName = strtolower(trim((string)$attributeName));
72 $attribVal = trim((string)$attributeValue);
74 $attribName = $ns . ':' . $attribName;
76 $attributes[$attribName] = $attribVal;
80 $objChildren = $obj->children($ns, true);
81 foreach( $objChildren as $childName=>$child ) {
82 $childName = strtolower((string)$childName);
84 $childName = $ns.':'.$childName;
86 $children[$childName][] = xmlObjToArr($child, $namespace);
94 $tableau['text'] = $text;
96 $tableau['attributes'] = $attributes;
98 $tableau['children'] = $children;