142b858e7e66489dec395d0647b06d6b6fcf59e2
[lhc/web/www.git] / www / ecrire / inc / simplexml_to_array.php
1 <?php
2
3 if (!defined("_ECRIRE_INC_VERSION")) return;
4
5
6 /**
7 * Transforme un objet SimpleXML en tableau PHP
8 *
9 * @param object $obj
10 * @return array
11 **/
12 // http://www.php.net/manual/pt_BR/book.simplexml.php#108688
13 // xaviered at gmail dot com 17-May-2012 07:00
14 function inc_simplexml_to_array_dist($obj, $utiliser_namespace='false') {
15
16 $tableau = array();
17
18 // Cette fonction getDocNamespaces() est longue sur de gros xml. On permet donc
19 // de l'activer ou pas suivant le contenu supposé du XML
20 if (is_object($obj)) {
21 if ($utiliser_namespace)
22 $namespace = $obj->getDocNamespaces(true);
23 $namespace[NULL] = NULL;
24
25 $name = strtolower((string)$obj->getName());
26 $text = trim((string)$obj);
27 if (strlen($text) <= 0) {
28 $text = NULL;
29 }
30
31 $children = array();
32 $attributes = array();
33
34 // get info for all namespaces
35 foreach( $namespace as $ns=>$nsUrl ) {
36 // attributes
37 $objAttributes = $obj->attributes($ns, true);
38 foreach( $objAttributes as $attributeName => $attributeValue ) {
39 $attribName = strtolower(trim((string)$attributeName));
40 $attribVal = trim((string)$attributeValue);
41 if (!empty($ns)) {
42 $attribName = $ns . ':' . $attribName;
43 }
44 $attributes[$attribName] = $attribVal;
45 }
46
47 // children
48 $objChildren = $obj->children($ns, true);
49 foreach( $objChildren as $childName=>$child ) {
50 $childName = strtolower((string)$childName);
51 if( !empty($ns) ) {
52 $childName = $ns.':'.$childName;
53 }
54 $children[$childName][] = inc_simplexml_to_array_dist($child);
55 }
56 }
57
58 $tableau = array(
59 'name'=>$name,
60 'text'=>$text,
61 'attributes'=>$attributes,
62 'children'=>$children
63 );
64 }
65
66 return $tableau;
67 }
68
69 ?>