Create file and force download

I want to create an xml sitemap , I made a php script that creates it … but the created xml code is saved inside a variable named as $xml

Now I want this variable to be saved inside a file named as sitemap.xml and force the browser to download it , Any help ?

Thanks fro any trial to help.
Dr Mostafa