sebastianbergmann/phpunit · error · PHPUnit\Util\Xml\XmlException

Could not parse XML from empty string

Error message

Could not parse XML from empty string

What it means

Xml\Loader::load() parses an XML string (not a file) into a DOMDocument. An empty input string is rejected immediately with XmlException('Could not parse XML from empty string'), before DOMDocument ever sees it. This is the programmatic counterpart of the empty-file error and guards callers from meaningless parse attempts.

Source

Thrown at src/Util/Xml/Loader.php:68

        if (trim($contents) === '') {
            throw new XmlException(
                sprintf(
                    'Could not parse XML from empty file "%s"',
                    $filename,
                ),
            );
        }

        return $this->load($contents, $ignoreComments);
    }

    /**
     * @throws XmlException
     */
    public function load(string $actual, bool $ignoreComments = false): DOMDocument
    {
        if ($actual === '') {
            throw new XmlException('Could not parse XML from empty string');
        }

        $document                     = new DOMDocument;
        $document->preserveWhiteSpace = false;

        $internal  = libxml_use_internal_errors(true);
        $message   = '';
        $reporting = error_reporting(0);
        $loaded    = $document->loadXML($actual, LIBXML_NONET);

        foreach (libxml_get_errors() as $error) {
            $message .= "\n" . $error->message;
        }

        libxml_use_internal_errors($internal);
        error_reporting($reporting);

        if ($loaded === false) {

View on GitHub (pinned to f123cdb2a2)

Solutions

  1. Check the string is non-empty before loading: if (trim($xml) === '') { /* handle missing XML */ }
  2. Trace where the empty string came from — usually a failed file_get_contents() or an unset optional config value
  3. Fall back to a valid default document (for example '<phpunit/>') when the XML is optional
  4. Add a unit test for the empty-input branch of your wrapper so it fails loudly where it is produced

Example fix

// before
$xml = file_get_contents($path) ?: '';
$document = (new \PHPUnit\Util\Xml\Loader)->load($xml);
// XmlException: Could not parse XML from empty string

// after
$xml = file_get_contents($path);
if ($xml === false || trim($xml) === '') {
    throw new InvalidArgumentException("No XML found at {$path}");
}
$document = (new \PHPUnit\Util\Xml\Loader)->load($xml);
Defensive patterns

Strategy: validation

Validate before calling

static function assertNonEmptyXmlString(string $xml): void
{
    if (trim($xml) === '') {
        throw new InvalidArgumentException('XML string is empty');
    }
}

Type guard

static function isParsableXmlString(string $xml): bool
{
    return trim($xml) !== '' && str_starts_with(ltrim($xml), '<');
}

Try / catch

use PHPUnit\Util\Xml\XmlException;

try {
    $document = (new Loader)->load($xml);
} catch (XmlException $e) {
    if ($e->getMessage() === 'Could not parse XML from empty string') {
        // the producer of $xml failed; fetch/regenerate before retrying
    }
    throw $e;
}

Prevention

When it happens

Trigger: Calling $loader->load($xml) with '' — typically because the string was built from a variable that was never set, a file_get_contents() that returned false and was cast to string, or a filtered/composed string that ended up empty.

Common situations: Tooling built on top of PHPUnit that assembles XML dynamically (baselines, generated configs); optional XML from environment or database that is absent; string casts of failed I/O calls.

Related errors


AI-assisted analysis of sebastianbergmann/phpunit@f123cdb2a2 (2026-08-23). Data as JSON: /api/errors/1dc29370b9905cf2. Report an issue: GitHub.