Cannot modify header information – headers already sent by (output started at / [duplicate]

function display_custom_page_content() { 


$current_url = strtolower($_SERVER['REQUEST_URI']);

$last_part = basename($current_url); 

$url = get_query_var('posts');

if ($last_part == $url && $url) { 

    // Output your custom HTML content here for your custom post type
    ob_start();

    include_once(plugin_dir_path(__FILE__) . 'relatedpost.php');

    ob_end_flush();

  generate_xml_callback();

    exit;

}

  }
add_action('template_redirect', 'display_custom_page_content');

 function generate_xml_callback() {

ob_start();

// Your predefined set of URLs
$urls = array(
    'https://plezententerprise.com/storys/page1',
    'https://plezententerprise.com/storys/page2',
    'https://plezententerprise.com/storys/page3',
    // Add more URLs as needed
);

// Create a new XML document
$xml = new DOMDocument('1.0', 'UTF-8');

$xml->formatOutput = true;

// Create the root element (urlset)
$urlset = $xml->createElement('urlset');

$urlset->setAttribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9');

$xml->appendChild($urlset);

// Add predefined URLs to the sitemap

foreach ($urls as $url) {

    $urlElement = $xml->createElement('url');

    $locElement = $xml->createElement('loc', htmlspecialchars($url));

    $urlElement->appendChild($locElement);

    // Add other optional elements like lastmod, changefreq, priority if needed

    $urlset->appendChild($urlElement);
}

// Set the HTTP header for content type
header('Content-Type: application/xml; charset=UTF-8');

// Echo the XML data
echo $xml->saveXML();

// Flush the output buffer
ob_flush();

// Exit to prevent further output
exit;

}

// Register the REST API endpoint for XML generation
add_action(‘rest_api_init’, function () {

register_rest_route('smg/v1', 'generate-xml', array(

    'methods' => 'GET',
    'callback' => 'generate_xml_callback',
    'permission_callback' => '__return_true',

));

  });

i am create A Plugin  

Cannot modify header information – headers already sent by (output started at /home/plezent/domains/plezententerprise.com/public_html/wp-content/plugins/storyhandler125 (1)/my-custom-post-creator.php:256) in /home/plezent/domains/plezententerprise.com/public_html/wp-content/plugins/storyhandler125