XML Parse Error - Extra content at the end of the document

XML Parse Error - Extra content at the end of the document

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

Stefan Bogdanescu

Founder & Senior Architect

XML Parse Error - Extra content at the end of the document

When building an API that supports multiple output formats, switching between JSON and XML is a common requirement. Developers often assume that if the data structure is valid in one format, it will translate seamlessly to another. However, as seen in many real-world scenarios involving legacy PHP applications, the transition can break due to subtle differences in how these two formats handle content termination.

The specific error message "Extra content at the end of the document" is a strict XML validation failure. It indicates that the XML parser has already finished processing a well-formed element when it encounters unexpected characters or tags immediately following it. Unlike JSON, which is generally more forgiving regarding trailing data in loose implementations, XML parsers are notoriously rigid. They expect the document to end exactly where the root element closes.

In the provided code example from inc.settings.php, the logic for generating the XML response relies on a custom function arrayToXML(). The generated string is stored in ${'Results'} and subsequently echoed in index.php after explicitly declaring an XML header.

echo "<?xml version=\"1.0\"?>";
echo ${'Results'};

This approach works for standard, pure XML data. However, the root cause of the parse error lies in how the application handles failure states and response bodies. The code initializes a $Response array that contains both a status field and a reason array (or nulls them out). When the API request fails—such as when an invalid API key is provided—the logic sets ${'Response'}['status'] = 'failed' and populates ${'Response'}['reason'][] = 'invalid api key'.

When this specific failure state is converted to XML via arrayToXML(), the resulting string looks something like this:

<response><status>failed</status><reason>invalid api key</reason></response>

If the application logic does not strictly strip the root element wrapper or if the arrayToXML function includes additional metadata (like a default namespace declaration) that conflicts with the manually echoed XML header, the parser will choke. More critically, if the arrayToXML function returns a string that wraps the data in tags but the application logic inadvertently appends whitespace, newlines, or BOMs after the closing tag, the parser throws the "extra content" error.

A frequent pitfall in PHP XML generation is the interaction between json_encode and arrayToXML logic within the same request lifecycle. In the snippet provided, if ${'Format Type'} evaluates to false, the code defaults back to JSON. However, if a developer accidentally sets ${'Format Type'} to true but fails to sanitize the $Response array before passing it to the XML converter, the resulting string might contain trailing commas or incomplete tags if the reason array logic isn't perfectly balanced.

To resolve this, you must ensure that the raw output passed to the browser is strictly a single root element with no subsequent characters. The most robust solution involves sanitizing the $Response array specifically for XML generation before passing it to the converter. Ensure that arrayToXML() returns a string that ends immediately after the closing bracket of the root node, without any trailing whitespace or hidden characters introduced by echo.

Furthermore, consider using dedicated libraries like SimpleXML or DOMDocument instead of custom array-to-XML functions. These libraries provide built-in methods to serialize data into well-formed XML strings and handle encoding issues automatically. For instance, using DOMDocument allows you to load the response object directly:

$dom = new DOMDocument('1.0', 'UTF-8');
// Load or construct elements here...
echo $dom->saveXML();

This approach guarantees that the output is strictly conformant to XML standards, eliminating issues caused by manual string concatenation errors in legacy arrayToXML implementations. Always verify that the Content-Type header remains set to application/xml and not accidentally overridden by PHP's default output buffering or other headers sent during error logging.

Our network