I see from the comments that you're working from within a CMS framework and are unable to stop content from being output prior to where your code will be.
If the script in which you're working has already output content (beyond your control), then you can't do what you're trying to achieve in just one script.
Your script can either send headers saying "the following content is HTML" then output the HTML or send headers saying "the following content is XML, is an attachment and has a certain filename". You can't do both.
You can either output HTML containing a link to a separate script for downloading an XML file or you can issue a file download and output no HTML.
Therefore, you'll have to add a download link in the output of the CMS script you're modifying and then handle the download in a separate script.
I have made a working example that should help. The example includes a simple HTML document containing a download link, and a PHP script that then handles the download.
View the code below or take a look at the live example.
HTML (extraneous fluff removed, not necessarily valid)
<html>
<head>
<title>XML Download Example</title>
</head>
<body>
<a href="download.php">Download XML example</a>
</body>
</html>
PHP
<?php
// Populate XML document
$doc = new DomDocument();
// ... various modifications to the document are made
// Output headers
header('Content-type: "text/xml"; charset="utf8"');
header('Content-disposition: attachment; filename="example.xml"');
// Output content
echo $doc->saveXML();
?>
If you are fully unable to handle the download via a second script (perhaps you can't get access to the relevant data), you'll have to re-think the problem.