/** * Simple recursive function to build an XML response. */ public static function xml_encode($k, $v) { if (is_object($v) && strtolower(get_class($v)) == 'simplexmlelement') { return preg_replace('/<\\?xml(.*?)\\?>/', '', $v->asXML()); } $res = ''; $attrs = ''; if (!is_numeric($k)) { $res = '<' . $k . '{{attributes}}>'; } if (is_array($v)) { foreach ($v as $key => $value) { if (strpos($key, '@') === 0) { $attrs .= ' ' . substr($key, 1) . '="' . $this->_xml_entities($value) . '"'; continue; } $res .= RestUtils::xml_encode($key, $value); $keys = array_keys($v); if (is_numeric($key) && $key != array_pop($keys)) { $res .= '</' . $k . ">\n<" . $k . '>'; } } } else { $res .= RestUtils::_xml_entities($v); } if (!is_numeric($k)) { $res .= '</' . $k . ">\n"; } $res = str_replace('<' . $k . '{{attributes}}>', '<' . $k . $attrs . '>', $res); return $res; }