How to display XML in HTML in PHP?

How to display XML in HTML in PHP?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Rendering XML as Preformatted Text in PHP

Displaying raw XML content within an HTML page presents a specific challenge: you need to preserve the structural integrity of the markup, including newlines and indentation, while preventing the browser from interpreting that text as actual document structure. The goal is to render the string exactly as it exists in memory, ensuring tags appear as text rather than elements, and maintaining the visual layout defined by tabs or spaces.

The fundamental tool for this task in PHP is the htmlspecialchars function. This built-in utility converts special characters like <, >, &, ", and ' into their corresponding HTML entities. By escaping these characters, you ensure that the browser renders them as literal text symbols rather than parsing them as HTML tags.

However, applying htmlspecialchars directly to a string often collapses whitespace. If your XML string contains newlines (\n) or tab characters intended for formatting, a naive application of this function might result in a single block of text where every line break is ignored by the browser's rendering engine. To achieve the specific requirement of displaying formatted XML with proper new lines and tabs without using external libraries like SimpleXML or DOMDocument (which would parse and potentially alter the structure), you must handle whitespace preservation explicitly.

Consider the following PHP implementation:

$xmlString = "
<shoes>
    <shoe>
       <shouename>Shoue</shouename>
    </shoe>
</shoes>
";

// 1. Preserve newlines and tabs by replacing them with HTML entities or non-breaking spaces if desired, 
// but for raw text preservation, we often need to ensure the browser respects them.
// The most direct way to force a line break in HTML is <br>, but that breaks the XML look.
// A better approach for "raw" display without external libraries is often:
// - Using htmlspecialchars to escape entities.
// - Ensuring whitespace is not trimmed before escaping.

$sanitized = htmlspecialchars($xmlString, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');

echo "<pre>" . $sanitized . "</pre>";

The critical element here is the <pre> HTML tag. This tag instructs the browser to respect whitespace and line breaks exactly as they appear in the source code. Without wrapping the escaped string in <pre>, the browser would likely collapse multiple spaces into one and ignore newlines, rendering the XML as a dense paragraph. Using htmlspecialchars inside a <pre> block effectively neutralizes the special characters so they display as text, while allowing the whitespace characters within the string to dictate the layout.

If you require specific formatting where tabs are converted to visual indentation that aligns perfectly with your theme's font width, you might consider using the HTML entity for a tab (&#9;) instead of literal \t characters, or ensuring your PHP string uses actual newline characters rather than escaped ones. However, standard htmlspecialchars preserves the literal characters \n if they are present in the string variable before escaping.

One common pitfall is modifying the string to remove leading or trailing whitespace before processing. Since the original example shows indentation within the XML structure, any operation that trims the outer boundaries of the string will ruin the visual hierarchy of the tags. You must ensure the entire buffer, including the indentation inside the root element, passes through the escaping function intact.

While frameworks like Laravel provide helpers for rendering content safely, keeping this logic in plain PHP ensures maximum portability and avoids dependency bloat. If you are working within a Laravel environment, you might find that e($string) is less reliable than explicitly using htmlspecialchars to guarantee consistent behavior across different Blade contexts. For strict control over the output without external libraries, the combination of htmlspecialchars and the <pre> tag remains the most robust solution for displaying raw XML text with preserved formatting.

Our network