How to use XMLReader/DOMDocument with large XML file and prevent 500 error

青春壹個敷衍的年華 提交于 2019-11-30 17:04:18

Probably you are running out of memory. Try to increase your memory limit

ini_set('memory_limit','128M');

or whatever the amount of memory is neccesary (it depends on your server). I leave you here some links with other ways of increasing the memory limit of your server:

PHP: Increase memory limit

PHP: Increase memory limit 2

Why are you mixing XMLReader / DomDocument? Just use XMLReader:

$reader = new XMLReader(); // initialize
$reader->open( 'file.xml' ); // open file
do {
    $sxe = simplexml_load_string( $reader->readOuterXml() ); // get current element
    echo $sxe; // echo current element
}
while( $reader->next( $this->type ) ); // repeat this for any "product" tag

The advantage of the example above is, that XMLReader will only read the current tag into memory. DomDocument reads the whole file - this is why you get error 500. With the given example you can handle XML files with hundreds of MB, without increasing your memory limit (except the current tag you try to read is greater then the available memory).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!