instead of echoing all the output
such as
echo '<?xml version="1.0" encoding="ISO-8859-1" ?>';
you could create a string
$output .= '<?xml version="1.0" encoding="ISO-8859-1" ?>';
then continue to add to the string for each item.
finally
$file = fopen("rss.xml",w+);
fwrite($file,$output);
fclose($file);
but check the manual for the details of the fwrite function