0

这里完全缺乏合适的解决方案让我发疯。

一位客户给了我一个大小为 14MB 的 XML 文件,在以后的请求中可能会更大。我需要一个可以定期将其数据加载到 MySQL 表中的脚本。服务器运行 MySQL 5.0.96,所以我不相信 LOAD XML 命令对我可用。

我曾尝试使用 PHP DOM 库来解析 XML 文件,但 Apache 返回 500 内部服务器错误。我认为这是内存不足,因为我可以注释掉加载 XML 文件的 PHP 代码并且脚本运行顺利(没有数据)。服务器上的内存限制已经是 128MB。

我也尝试过 XMLReader,它似乎也在做同样的事情。有什么建议么?

4

1 回答 1

0

XMLReader 用作迭代器,因此您必须根据 xml 文档的各个标记来实现分组/提取逻辑。假设<row>您的 xml 文件中有一个元素对应于单个插入,它可能看起来像这样:

$reader = new XMLReader();
$reader->open('/path/to/the/file.xml');
while ($reader->read()) {
    if ($reader->name == "row" && $reader->nodeType == XMLReader::ELEMENT) {
        // the cursor currently at <row> element

        // hopefully one fragment will fit into memory and you can use simplexml for easy access
        // alternatively try $reader->expand() for DOMNode of the same thing
        $rowxml = simplexml_load_string('<'.'?xml version='1.0'?'.'>'.$reader->readOuterXML());

        // build insert from the $rowxml object, run the query
    }
}
$reader->close();
于 2012-11-21T20:10:24.760 回答