Skip to content

DomDocument::loadXML cannot parse files larger than 1,3 GiB #14684

@pjpawel

Description

@pjpawel

Description

The following code:

<?php
function loadXMLFile($filename) {

    $content = file_get_contents($filename);

    $doc = new DOMDocument();
    
    $success = $doc->loadXML($content);

    if (!$success) {
        echo "Failed to parse $filename\n";
        return null;
    }

    return $doc;
}

function main($filename) {
    $doc = loadXMLFile($filename);

    if ($doc !== null) {
        $root = $doc->documentElement;
        echo "Root element: " . $root->nodeName . "\n";
    } else {
        echo "Empty document or failed to load.\n";
    }
}

if ($argc != 2) {
    echo "Usage: php " . $argv[0] . " <xmlfile>\n";
    exit(1);
}

main($argv[1]);

Resulted in this output:

PHP Warning:  DOMDocument::loadXML(): Memory allocation failed : growing input buffer in domLoadXml.php on line 8

Warning: DOMDocument::loadXML(): Memory allocation failed : growing input buffer in domLoadXml.php on line 8
Root element: tns:JPK

But I expected this output instead:

Root element: tns:JPK

I tried using option LIBXML_PARSEHUGE, but result was the same.
The most curious thing is that DomDocument::load() is loading large files correctly.

PHP Version

8.2.5

Operating System

Windows 11, Ubuntu 22.04

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions