Question: So I'm trying to find a way to download a report from salesforce programmatically, to a local machine.

so, I've found the XMLStreamWriter class, and am writing the XML just fine... I'm just wondering if it is possible to save this XML string to a file and then downloading it. Has anyone done something similar to this before? Any pointers/examples would be awesome!

Answer: 
I've worked on this multiple times recently for a client who needs to output various fixed width file formats - not quite the same situation as you, but once you've got yourself down to a string it's a piece of cake.

Check out this Gist for some boilerplate to get you going: <https://gist.github.com/Oblongmana/78c2807c91903bfe22b9>

So say you have a String called 'txt':

```
    Blob txtBlob = Blob.valueOf(txt); //Convert it to a blob

    Attachment attach = new Attachment(); //Make an attachment
    attach.Name ='text.xml';  
    attach.Body = txtBlob;
    attach.ContentType= 'application/xml'; //Signal what the file's MIME type is
    attach.ParentID = [parentIDGoesHere];
    insert attach;

```

You can then serve this file up like so: 

```
    <a href="/servlet/servlet.FileDownload?file=' + attach.ID +'">Download me!</a>

```
Although the above answer is the better solution IMO (and I have upvoted), this is an alternative solution I helped create a few years back. The scenario was similar, a fixed-width file needed to be generated and extracted twice a day.

The solution was to build a custom object where each record stored a line of the file - seems odd but the file could be of varying sizes and there were different 'types' of line so the ordering was important too.

Twice a day a batch file (it had to be a Windows box unfortunately) would fire up dataloader to pull out the lines in the correct order and then it'd use a command line substitution tool (similar to `sed`) to do a bit of cleanup (removing the CSV parts) before moving the file into another directory where it was needed.

The batch would then use the success log to update the downloaded records setting a checkbox to true, and those would subsequently be deleted from the system by a scheduled batch apex job.