/** * converts a string containing xml into an array * @param string to be unescaped * @param bool (default false) true to recurse * @return array */ static function ToArray($xmlstring, $recurse = false) { $xml = VerySimpleXmlUtil::SafeParse($xmlstring); $array = array(); if ($recurse) { VerySimpleXmlUtil::RecurseXmlObjToArr($xml, $array); } else { foreach ($xml as $key => $val) { $array[strval($key)] = strval($val); } } return $array; }
/** * converts a string containing xml into an array. Note that if $recurse is false * This this will return a more simple structure but will only parse up to 3 levels * @param string to be unescaped * @param bool (default false) true to recurse * @param string $emptyVal if $xml is empty, default to this value (ex "<xml/>") * @return array */ static function ToArray($xmlstring, $recurse = false, $emptyVal = null) { $xmlstring = trim($xmlstring); if (!$xmlstring) { $xmlstring = $emptyVal; if (!$xmlstring) { throw new Exception('Empty string could not be parsed as XML'); } } $xml = VerySimpleXmlUtil::SafeParse($xmlstring); $array = array(); if ($recurse) { VerySimpleXmlUtil::RecurseXmlObjToArr($xml, $array); } else { foreach ($xml as $key => $val) { $children = $val->children(); if ($children) { $grandchildren = $children->children(); if ($grandchildren) { $array[$key] = $children; } else { $array[] = $val; } } else { $array[strval($key)] = strval($val); } } } return $array; }