function XMLdata($data, $rootNodeName = 'data', $xml = null)
{
    ini_set('zend.ze1_compatibility_mode', 0);
    # turn off SimpleXML complains
    if ($xml == null) {
        $xml = simplexml_load_string("<?xml version='1.0' encoding='utf-8'?><{$rootNodeName} />");
    }
    if (!is_array($data)) {
        $xml->addChild('scalar', $data);
    } else {
        foreach ($data as $key => $value) {
            $keystr = is_numeric($key) ? 'key' : $key;
            # No numeric keys
            #$key = preg_replace('/[^a-z0-9_:-]/i', '', $key); # key format
            if (is_array($value)) {
                # deeper!
                $node = $xml->addChild($keystr);
                XMLdata($value, $rootNodeName, $node);
                # recursive
            } else {
                $value = htmlentities($value);
                $node = $xml->addChild($keystr, $value);
            }
            if (is_numeric($key)) {
                $node->addAttribute('i', $key);
            }
        }
    }
    return $xml;
}
} catch (NoControllerRouteException $e) {
    return http_error400('Unknown controller: ' . $e->getMessage());
} catch (NoMethodRouteException $e) {
    return http_error400('Unknown method: ' . $e->getMessage());
} catch (ParamRequiredRouteException $e) {
    return http_error400('Missing param: ' . $e->getMessage());
} catch (ShouldBeArrayRouteException $e) {
    return http_error400('Param should be an array: ' . $e->getMessage());
} catch (ActionException $e) {
    return http_error400($e->getMessage());
}
// Format the response
switch ($components['.']) {
    case 'dump':
        var_dump($response);
        break;
    case 'php':
        print var_export($response, 1);
        break;
    case 'json':
        header('Content-Type: application/json;charset=UTF-8');
        header('Allow-Origin: *');
        print json_encode($response);
        break;
    case 'xml':
        header('Content-Type: application/xml;charset=UTF-8');
        print XMLdata($response, $components['action'])->asXML();
        break;
    default:
        return http_error400('Unsupported format');
}