public static function decode($data, $preserve = false)
{
$nodes = array();
if ($preserve) {
$matches = array();
preg_match_all('/<!\\[cdata\\[(.*?)\\]\\]>/is', $data, $matches);
foreach ($matches[0] as $match) {
$strip = str_replace(array('<![CDATA[', ']]>', '<', '>'), array('', '', '<', '>'), $match);
$data = str_replace($match, $strip, $data);
}
$nodes = json_decode(json_encode((array) simplexml_load_string($data)), true);
} else {
$xml = new \SimpleXMLElement($data);
$i = 1;
foreach ($xml as $key => $node) {
$objs = array();
foreach ($node as $k => $v) {
$j = 1;
if (array_key_exists((string) $k, $objs)) {
while (array_key_exists($k . '_' . $j, $objs)) {
$j++;
}
$newKey = (string) $k . '_' . $j;
} else {
$newKey = (string) $k;
}
$objs[$newKey] = trim((string) $v);
}
$nodes[$key . '_' . $i] = $objs;
$i++;
}
}
return $nodes;
}