Site icon Hip-Hop Website Design and Development

Downloading Generated XML File

I’m making a Widget which presents users with categories as drop down list. When user selects category and press download, I want to get all posts under category and download them. I have faced a problem on downloading.
I do something like

$xml = ''; //generated XML with Simple XML
header('Content-type: text/xml');
header('Content-Disposition: attachment; filename="text.xml"');
echo $xml->asXML(); 

I get headers already sent. So I added buffering to init with code:

add_action('init', 'do_output_buffer');

function do_output_buffer() {
        ob_start();
}

and in my MY_Widget::widget() I add the code echo ob_get_clean(); after echo $xml->asXML(); explained above. Here it works except it exports whole page (with my XML) as single XML file.

Is there a way to download file without including whole page?