Php Laravel - How to show xml file in browser

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Php Laravel: How to Display XML Files in the Browser Correctly

As a senior developer, I often encounter scenarios where we generate structured data—like XML or JSON—on the backend using PHP and need to present it correctly to the user's browser. The confusion often stems not from how to generate the file, but how to instruct the browser on how to interpret that generated stream of bytes.

You are asking how to take an XML file you generate in PHP and display it in a web browser, perhaps for use as a sitemap or any other structured data presentation. The short answer is: it requires setting the correct HTTP headers. Simply reading the file content is not enough; you must tell the client (the browser) what kind of content it is receiving.

Let’s dive into the developer perspective on correctly serving XML data in a Laravel environment.


Understanding HTTP Headers for XML Delivery

When a web server sends data to a browser, it uses HTTP headers to define the nature of that data. If you send raw text without proper instruction, the browser might try to render it as HTML, leading to broken or nonsensical output.

To correctly serve an XML file, you need to set the Content-Type header to application/xml. This tells the browser, "The data coming next is structured XML, not plain HTML."

Here is the fundamental principle in PHP:

header('Content-Type: application/xml');
// Now output the XML content...
echo $xml_string;

If you omit this header or use an incorrect one (like text/plain), the browser will display the raw XML tags, which might look messy but is technically correct for a machine reading it. However, if your goal is to present readable information (like a sitemap), simply dumping the raw XML might not be user-friendly.

Implementing Dynamic XML Generation in Laravel

In a modern framework like Laravel, we typically handle this logic within a Controller. We can leverage PHP's built-in XML handling capabilities or use dedicated libraries.

Let’s refine your approach using SimpleXMLElement which you were already employing. The key is ensuring the output stream is correctly managed.

Code Example: Correct XML Output

Instead of relying on reading and dumping a file, it is cleaner and more efficient to generate the XML string directly in memory and then send it with the right headers.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use SimpleXMLElement;

class SitemapController extends Controller
{
    public function siteMap()
    {
        // 1. Data preparation (using your logic)
        $test_array = [
            'bla' => 'blub',
            'foo' => 'bar',
            'another_array' => [
                'stack' => 'overflow',
            ],
        ];

        // 2. Initialize the root element
        $xml_template_info = new \SimpleXMLElement("<?xml version=\"1.0\"?><template></template>");

        // 3. Populate the structure (assuming you have an array_to_xml helper)
        $this->array_to_xml($test_array, $xml_template_info);

        // Get the final XML string
        $xml_output = $xml_template_info->asXML();

        // 4. Set the correct content type header
        header('Content-Type: application/xml');

        // 5. Output the result
        echo $xml_output;
    }

    /**
     * Helper function to recursively build XML structure.
     */
    protected function array_to_xml(array $arr, \SimpleXMLElement $xml): void
    {
        foreach ($arr as $k => $v) {
            if (is_array($v)) {
                // Recurse for nested arrays
                $xml->addChild($k, $this->array_to_xml($v, $xml->addChild($k)));
            } else {
                // Add simple elements
                $xml->addChild($k, $v);
            }
        }
    }
}

Notice how we explicitly call header('Content-Type: application/xml'); before outputting the content. This is a critical step for proper web communication. Frameworks like Laravel encourage this approach when using the response() helper, ensuring clean separation between data logic and presentation. For more complex tasks involving file management or routing, understanding these principles helps you build robust applications, much like the structure enforced by frameworks like Laravel.

Beyond Raw XML: When to Use JSON

While XML is excellent for strict data exchange (like RSS feeds or SOAP services), for modern web applications where the goal is simply to display structured data in a browser, JSON (JavaScript Object Notation) is often superior.

JSON is natively understood by JavaScript and is much easier to parse on the frontend. If your primary goal is displaying a sitemap structure to users, consider generating the data as an array of objects and outputting it as JSON:

[
  {"url": "/page1", "lastmod": "2023-10-26"},
  {"url": "/page2", "lastmod": "2023-10-25"}
]

You would use PHP's json_encode() function and set the header to Content-Type: application/json. This approach aligns better with modern API design patterns, which is a core philosophy in building scalable applications on platforms like Laravel.

Conclusion

To summarize, showing an XML file in a browser requires more than just generating the XML content; it demands precise handling of HTTP communication. Always set the Content-Type header to application/xml. For dynamic generation within a framework like Laravel, focus on building your data structure first and then use the appropriate output function (echo or response()) with the correct MIME type. If presentation is the goal, weigh the benefits of using JSON over XML for browser readability.