Parse large XML files in PHP


A good way to parse large XML documents when you can't load it in memory entirely is to use XMLReader API

This API allows you to parse XML files on-the-fly and expand it to DOM objects only where you need it:

$document = new DOMDocument('1.0', 'utf-8');
$reader = new XMLReader;

    if($reader->localName == 'book')
        $node = $reader->expand($document);

        if($node !== false)
            # $node has a type of DOMNode which you are familiar with
Share this page:

See also how to:

How to create a well-crafted RSS feed for a website and configure it property
How to make POST and other types of HTTP requests and add custom HTTP headers with file_get_contents function in PHP
How to pretty print XML documents on Linux
How to run your PHP or Bash script as root in the most secure way
How to use modern MaxMind GeoIP API in PHP