La forma más sencilla es usando SimpleXML para crearlo
Código PHP:
Ver original<?php
$xml = new SimpleXMLElement("<foo></foo>");
$xml->addChild('bar', 'bar');
for($i=0; $i<20; $i++){
$baz = $xml->addChild('baz');
$baz->addChild('candy', 'candy_' . $i);
$baz->addChild('fruits', 'fruits_' . $i);
}
header('Content-Type: text/xml');
/**
* Para que se vea bien al mirar el código fuente
* pero si no te importa con tan solo colocar la siguiente línea
* echo $xml->asXML();
* es suficiente
*/
$outXML = $xml->asXML();
$xml = new DOMDocument();
$xml->preserveWhiteSpace = false;
$xml->formatOutput = true;
$xml->loadXML($outXML);
echo $xml->saveXML();