note 108688 modified in book.simplexml by danbrown

From: Date: Thu, 17 May 2012 19:03:11 +0000
Subject: note 108688 modified in book.simplexml by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-188043@lists.php.net to get a copy of this message
Here is a recursive function that will convert a given SimpleXMLElement object into an array, preserving namespaces and attributes.&#13;&#10;&#13;&#10;&#60;?php&#13;&#10;function xmlObjToArr($obj) {&#13;&#10;&#9;&#9;$namespace = $obj-&#62;getDocNamespaces(true);&#13;&#10;&#9;&#9;$namespace[NULL] = NULL;&#13;&#10;&#9;&#9;&#13;&#10;&#9;&#9;$children = array();&#13;&#10;&#9;&#9;$attributes = array();&#13;&#10;&#9;&#9;$name = strtolower((string)$obj-&#62;getName());&#13;&#10;&#9;&#9;&#13;&#10;&#9;&#9;$text = trim((string)$obj);&#13;&#10;&#9;&#9;if( strlen($text) &#60;= 0 ) {&#13;&#10;&#9;&#9;&#9;$text = NULL;&#13;&#10;&#9;&#9;}&#13;&#10;&#9;&#9;&#13;&#10;&#9;&#9;// get info for all namespaces&#13;&#10;&#9;&#9;if(is_object($obj)) {&#13;&#10;&#9;&#9;&#9;foreach( $namespace as $ns=&#62;$nsUrl ) {&#13;&#10;&#9;&#9;&#9;&#9;// atributes&#13;&#10;&#9;&#9;&#9;&#9;$objAttributes = $obj-&#62;attributes($ns, true);&#13;&#10;&#9;&#9;&#9;&#9;foreach( $objAttributes as $attributeName =&#62; $attributeValue ) {&#13;&#10;&#9;&#9;&#9;&#9;&#9;$attribName = strtolower(trim((string)$attributeName));&#13;&#10;&#9;&#9;&#9;&#9;&#9;$attribVal = trim((string)$attributeValue);&#13;&#10;&#9;&#9;&#9;&#9;&#9;if (!empty($ns)) {&#13;&#10;&#9;&#9;&#9;&#9;&#9;&#9;$attribName = $ns . &#39;:&#39; . $attribName;&#13;&#10;&#9;&#9;&#9;&#9;&#9;}&#13;&#10;&#9;&#9;&#9;&#9;&#9;$attributes[$attribName] = $attribVal;&#13;&#10;&#9;&#9;&#9;&#9;}&#13;&#10;&#9;&#9;&#9;&#9;&#13;&#10;&#9;&#9;&#9;&#9;// children&#13;&#10;&#9;&#9;&#9; $objChildren = $obj-&#62;children($ns, true);&#13;&#10;&#9;&#9;&#9;&#9;foreach( $objChildren as $childName=&#62;$child ) {&#13;&#10;&#9;&#9;&#9;&#9;&#9;$childName = strtolower((string)$childName);&#13;&#10;&#9;&#9;&#9;&#9;&#9;if( !empty($ns) ) {&#13;&#10;&#9;&#9;&#9;&#9;&#9;&#9;$childName = $ns.&#39;:&#39;.$childName;&#13;&#10;&#9;&#9;&#9;&#9; }&#13;&#10;&#9;&#9;&#9;&#9;&#9;$children[$childName][] = xmlObjToArr($child);&#13;&#10;&#9;&#9;&#9;&#9;}&#13;&#10;&#9;&#9;&#9;}&#13;&#10;&#9;&#9;}&#13;&#10;&#9;&#9;&#13;&#10;&#9; return array(&#13;&#10;&#9;&#9; &#39;name&#39;=&#62;$name,&#13;&#10;&#9;&#9; &#39;text&#39;=&#62;$text,&#13;&#10;&#9;&#9; &#39;attributes&#39;=&#62;$attributes,&#13;&#10;&#9;&#9; &#39;children&#39;=&#62;$children&#13;&#10;&#9; );&#13;&#10;&#9;}&#13;&#10;?&#62; --was-- Here is a recursive function that will convert a given SimpleXMLElement object into an array, preserving namespaces and attributes. function xmlObjToArr($obj) { $namespace = $obj->getDocNamespaces(true); $namespace[NULL] = NULL; $children = array(); $attributes = array(); $name = strtolower((string)$obj->getName()); $text = trim((string)$obj); if( strlen($text) <= 0 ) { $text = NULL; } // get info for all namespaces if(is_object($obj)) { foreach( $namespace as $ns=>$nsUrl ) { // atributes $objAttributes = $obj->attributes($ns, true); foreach( $objAttributes as $attributeName => $attributeValue ) { $attribName = strtolower(trim((string)$attributeName)); $attribVal = trim((string)$attributeValue); if (!empty($ns)) { $attribName = $ns . ':' . $attribName; } $attributes[$attribName] = $attribVal; } // children $objChildren = $obj->children($ns, true); foreach( $objChildren as $childName=>$child ) { $childName = strtolower((string)$childName); if( !empty($ns) ) { $childName = $ns.':'.$childName; } $children[$childName][] = xmlObjToArr($child); } } } return array( 'name'=>$name, 'text'=>$text, 'attributes'=>$attributes, 'children'=>$children ); } http://php.net/manual/en/book.simplexml.php

« previous php.notes (#188043) next »