self::$maxRecursionDepthAllowed) { // XML tree is too deep. Exit now by throwing an exception. throw new Zend_Json_Exception( "Function _processXml exceeded the allowed recursion depth of " . self::$maxRecursionDepthAllowed); } // End of if ($recursionDepth > self::$maxRecursionDepthAllowed) if ($recursionDepth == 0) { // Store the original SimpleXmlElementObject sent by the caller. // We will need it at the very end when we return from here for good. $callerProvidedSimpleXmlElementObject = $simpleXmlElementObject; } // End of if ($recursionDepth == 0) if ($simpleXmlElementObject instanceof SimpleXMLElement) { // Get a copy of the simpleXmlElementObject $copyOfSimpleXmlElementObject = $simpleXmlElementObject; // Get the object variables in the SimpleXmlElement object for us to iterate. $simpleXmlElementObject = get_object_vars($simpleXmlElementObject); } // End of if (get_class($simpleXmlElementObject) == "SimpleXMLElement") // It needs to be an array of object variables. if (is_array($simpleXmlElementObject)) { // Initialize a result array. $resultArray = array(); // Is the input array size 0? Then, we reached the rare CDATA text if any. if (count($simpleXmlElementObject) <= 0) { // Let us return the lonely CDATA. It could even be // an empty element or just filled with whitespaces. return (trim(strval($copyOfSimpleXmlElementObject))); } // End of if (count($simpleXmlElementObject) <= 0) // Let us walk through the child elements now. foreach($simpleXmlElementObject as $key=>$value) { // Check if we need to ignore the XML attributes. // If yes, you can skip processing the XML attributes. // Otherwise, add the XML attributes to the result array. if(($ignoreXmlAttributes == true) && (is_string($key)) && ($key == "@attributes")) { continue; } // End of if(($ignoreXmlAttributes == true) && ($key == "@attributes")) // Let us recursively process the current XML element we just visited. // Increase the recursion depth by one. $recursionDepth++; $resultArray[$key] = self::_processXml ($value, $ignoreXmlAttributes, $recursionDepth); // Decrease the recursion depth by one. $recursionDepth--; } // End of foreach($simpleXmlElementObject as $key=>$value) { if ($recursionDepth == 0) { // That is it. We are heading to the exit now. // Set the XML root element name as the root [top-level] key of // the associative array that we are going to return to the original // caller of this recursive function. $tempArray = $resultArray; $resultArray = array(); $resultArray[$callerProvidedSimpleXmlElementObject->getName()] = $tempArray; } // End of if ($recursionDepth == 0) return($resultArray); } else { // We are now looking at either the XML attribute text or // the text between the XML tags. return (trim(strval($simpleXmlElementObject))); } // End of if (is_array($simpleXmlElementObject)) } // End of function _processXml. }