public static function convertSimpleXmlElementObjectIntoArray($simpleXmlElementObject, &$recursionDepth = 0) { if ($recursionDepth > MAX_RECURSION_DEPTH_ALLOWED) { return null; } if ($recursionDepth == 0) { if (get_class($simpleXmlElementObject) != SIMPLE_XML_ELEMENT_PHP_CLASS) { return null; } else { $callerProvidedSimpleXmlElementObject = $simpleXmlElementObject; } } if (@get_class($simpleXmlElementObject) == SIMPLE_XML_ELEMENT_PHP_CLASS) { $copyOfsimpleXmlElementObject = $simpleXmlElementObject; $simpleXmlElementObject = get_object_vars($simpleXmlElementObject); } if (is_array($simpleXmlElementObject)) { $resultArray = array(); if (count($simpleXmlElementObject) <= 0) { return trim(strval($copyOfsimpleXmlElementObject)); } foreach ($simpleXmlElementObject as $key => $value) { $recursionDepth++; $resultArray[$key] = xml2json::convertSimpleXmlElementObjectIntoArray($value, $recursionDepth); $recursionDepth--; } if ($recursionDepth == 0) { $tempArray = $resultArray; $resultArray = array(); $resultArray[$callerProvidedSimpleXmlElementObject->getName()] = $tempArray; } return $resultArray; } else { return trim(strval($simpleXmlElementObject)); } }
public static function convertSimpleXmlElementObjectIntoArray($simpleXmlElementObject, &$recursionDepth = 0) { // Keep an eye on how deeply we are involved in recursion. if ($recursionDepth > MAX_RECURSION_DEPTH_ALLOWED) { // Fatal error. Exit now. return null; } if ($recursionDepth == 0) { if (get_class($simpleXmlElementObject) != SIMPLE_XML_ELEMENT_PHP_CLASS) { // If the external caller doesn't call this function initially // with a SimpleXMLElement object, return now. return null; } else { // 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 (is_object($simpleXmlElementObject) and get_class($simpleXmlElementObject) == SIMPLE_XML_ELEMENT_PHP_CLASS) { // 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); } // It needs to be an array of object variables. if (is_array($simpleXmlElementObject)) { // Initialize the 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 whitespaces. return trim(strval($copyOfsimpleXmlElementObject)); } // Let us walk through the child elements now. foreach ($simpleXmlElementObject as $key => $value) { // When this block of code is commented, XML attributes will be // added to the result array. // Uncomment the following block of code if XML attributes are // NOT required to be returned as part of the result array. /* if((is_string($key)) && ($key == SIMPLE_XML_ELEMENT_OBJECT_PROPERTY_FOR_ATTRIBUTES)) { continue; } */ // Let us recursively process the current element we just visited. // Increase the recursion depth by one. $recursionDepth++; $resultArray[$key] = xml2json::convertSimpleXmlElementObjectIntoArray($value, $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 caller of this // recursive function. $tempArray = $resultArray; $resultArray = array(); $resultArray[$callerProvidedSimpleXmlElementObject->getName()] = $tempArray; } 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 else }
<?php $xml_file_path = 'vnstat.xml'; if (file_exists($xml_file_path)) { $xml = simplexml_load_file($xml_file_path); require_once 'lib/xml2json.php'; if ($xml_as_array = xml2json::convertSimpleXmlElementObjectIntoArray($xml)) { if ($json = json_encode($xml_as_array)) { header('Content-type: application/json'); echo $json; } else { exit("Could not convert array to JSON."); } } else { exit("Could not convert XML to array for conversion to JSON."); } } else { exit("Failed to load {$xml_file_path}."); }
public static function convertSimpleXmlElementObjectIntoArray($simpleXmlElementObject, &$recursionDepth = 0) { // Keep an eye on how deeply we are involved in recursion. if ($recursionDepth > MAX_RECURSION_DEPTH_ALLOWED) { // Fatal error. Exit now. return null; } if ($recursionDepth == 0) { if (get_class($simpleXmlElementObject) != SIMPLE_XML_ELEMENT_PHP_CLASS) { // If the external caller doesn't call this function initially // with a SimpleXMLElement object, return now. return null; } else { // 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 (is_object($simpleXmlElementObject) && get_class($simpleXmlElementObject) == SIMPLE_XML_ELEMENT_PHP_CLASS) { // Get a copy of the simpleXmlElementObject $copyOfsimpleXmlElementObject = $simpleXmlElementObject; } // It needs to be an array of object variables. if ($simpleXmlElementObject->count() > 0) { // Initialize the result array. $resultArray = array(); //Add the attributes if any //Now add the attributes foreach ($simpleXmlElementObject->attributes() as $key => $value) { $resultArray[SIMPLE_XML_ELEMENT_OBJECT_PROPERTY_FOR_ATTRIBUTES][$key] = trim(strval($value)); } // Let us walk through the child elements now. foreach ($simpleXmlElementObject->children() as $child) { // When this block of code is commented, XML attributes will be // added to the result array. // Uncomment the following block of code if XML attributes are // NOT required to be returned as part of the result array. /* if((is_string($key)) && ($key == SIMPLE_XML_ELEMENT_OBJECT_PROPERTY_FOR_ATTRIBUTES)) { continue; } */ // Let us recursively process the current element we just visited. // Increase the recursion depth by one. $recursionDepth++; //Group children with the same name if (isset($resultArray[$child->getName()])) { //If it is array apppend if (is_array($resultArray[$child->getName()]) && !isset($resultArray[$child->getName()][SIMPLE_XML_ELEMENT_OBJECT_PROPERTY_FOR_TXT_VALUE])) { //If it contains @value then is not really an array $resultArray[$child->getName()][] = xml2json::convertSimpleXmlElementObjectIntoArray($child, $recursionDepth); } else { //else convert and append $resultArray[$child->getName()] = array(0 => $resultArray[$child->getName()]); $resultArray[$child->getName()][] = xml2json::convertSimpleXmlElementObjectIntoArray($child, $recursionDepth); } } else { $resultArray[$child->getName()] = xml2json::convertSimpleXmlElementObjectIntoArray($child, $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 caller of this // recursive function. $tempArray = $resultArray; $resultArray = array(); $resultArray[$callerProvidedSimpleXmlElementObject->getName()] = $tempArray; } return $resultArray; } else { // We are now looking at either the XML attribute text or // the text between the XML tags. //Check for attributes, it it has attritubes return an @array containing @atrtibutes and @value if (count($simpleXmlElementObject->attributes()) > 0) { $r_array = array(); foreach ($simpleXmlElementObject->attributes() as $key => $value) { $r_array[SIMPLE_XML_ELEMENT_OBJECT_PROPERTY_FOR_ATTRIBUTES][$key] = trim(strval($value)); } $r_array[SIMPLE_XML_ELEMENT_OBJECT_PROPERTY_FOR_TXT_VALUE] = trim(strval($simpleXmlElementObject)); return $r_array; } else { //Else return the value return trim(strval($simpleXmlElementObject)); } } // End of else }