<?php
/*mime type para el contenido content*/
header('Content-type: text/xml');
function generate_xml_from_array($array, $nodeName)
{
    $xml = '';
    if (is_array($array) || is_object($array)) {
        foreach ($array as $key => $value) {
            if (is_numeric($key)) {
                $key = $nodeName;
            }
            $xml .= '<' . $key . '>' . "\n" . generate_xml_from_array($value, $nodeName) . '</' . $key . '>' . "\n";
        }
    } else {
        $xml = htmlspecialchars($array, ENT_QUOTES) . "\n";
    }
    return $xml;
}

function generate_valid_xml_from_array($array, $nodeBlock = 'nodes', $nodeName = 'node')
{
    $xml = '<?xml version="1.0" encoding="UTF-8" ?>' . "\n";
    $xml .= '<' . $nodeBlock  . '>' . "\n";
    $xml .= generate_xml_from_array($array, $nodeName);
    $xml .= '</' . $nodeBlock  . '>' . "\n";
    return $xml;
}

//First element
$first =  is_array($data) ? $data[key($data)] : $data;
$block =  $first == $data && is_object($first) ? strtolower(get_class($first)) : 'data';
$node  =  is_object($first) ? strtolower(get_class($first)) : 'node';

echo generate_valid_xml_from_array($data, $block, $node);
/*
TODO use Simple XML
function printElem($xml, $elem){
    $xml->startElement();
    $a = get_object_vars($elem);
    foreach($a as $key=>$value) {
        $xml->startElement($key);
        $xml->text($value);
        $xml->endElement();
    }
    $xml->endElement();
}

$xml=new XMLWriter();
$xml->openMemory();
$xml->startDocument('1.0','UTF-8');
$xml->startElement('xml');
$xml->setIndent(true);

if(is_array($data)){
    foreach($data as $elem) {
        printElem($xml, $elem);
    }
}else{
    printElem($xml, $data);
}

$xml->endElement();
echo $xml->outputMemory(true);
*/
