问题描述
我编写了一个应用程序,它使用 XMLReader 在 PHP 中解析大型 XML 文件.
I've written an application that parses large XML files in PHP using XMLReader.
通常,当我尝试读取大于 2gb 的文件时,应用程序运行良好,EXCEPT.
Generally, the application works great, EXCEPT when I attempt to read a file that's larger than 2gb.
(我还没有弄清楚截止点的确切位置;它在 500mb 的文件上完美运行,但在我拥有的下一个最大文件 - 2.5gb 上失败了).
(I haven't figured out exactly where the cut-off is; it works flawlessly on a 500mb file, but fails on the next largest file I have - 2.5gb).
具体来说,如果我的代码如下所示:
Specifically, if my code looks like this:
$reader = new XMLReader();
if ($reader->open("big.xml")) {
echo "Success!";
$reader->close();
} else {
echo "Failed!";
}
如果我测试大 (>2gb) 文件 - 我得到这个:
If I test the large (>2gb) file - I get this:
Warning: XMLReader::open() [xmlreader.open]: Unable to open source data in [php script]
当然,Failed!
是输出.
如果我尝试使用较小的 (500mb) 文件 - 我只会得到 Success!
输出.
If I try with a smaller (500mb) file - I get only the Succcess!
output.
据我所知 - 无法打开的大文件和可以打开的中型文件之间没有区别(例如权限、有效的 XML、编码)EXCEPT 文件的大小.
As far as I can tell - there's no difference between the large files that can't be opened and the medium-size files that can be opened (e.g. permissions, valid XML, encoding) EXCEPT the size of the file.
虽然文件很大 - 节点都很小,所以我认为任何单个节点都不会导致内存问题.
While the size of the file is large - the nodes are all tiny, so I don't think any single node would cause a memory issue.
推荐答案
PHP 可能内存不足.试试:
PHP may be running out of memory. Try:
ini_set('memory_limit','256M');
这篇关于XMLReader 无法打开大型 (>2gb) XML 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!