Read/Write large XMLs without using too much memory in PHP
I'm trying to query some very large XML files (up to several gig), check them for being well-formed, and write them to hard disk. Seems easy, but as they are so large I can not have a whole document in the memory. Therefore my question is: How can I read, check and write large XML files when less RAM is available than one document is in size?
My Approach: Read it node by node with XMLReader (if that succeeds the document must be well-formed) and use XMLWriter for writing. Problem: XMLWriter seems to store everything in RAM till the document is finalized. DOM Documents and SimpleXML seem to do the same. Is there anything else I could try?
Your approach seems suitable, to get around the RAM issue with XMLWriter you could try periodically flushing the memory into your output file.
<?php
$xmlWriter = new XMLWriter();
$xmlWriter->openMemory();
$xmlWriter->startDocument('1.0', 'UTF-8');
for ($i=0; $i<=10000000; ++$i) {
$xmlWriter->startElement('message');
$xmlWriter->writeElement('content', 'Example content');
$xmlWriter->endElement();
// Flush XML in memory to file every 1000 iterations
if (0 == $i%1000) {
file_put_contents('example.xml', $xmlWriter->flush(true), FILE_APPEND);
}
}
// Final flush to make sure we haven't missed anything
file_put_contents('example.xml', $xmlWriter->flush(true), FILE_APPEND);`
Source: http://codeinthehole.com/tips/creating-large-xml-files-with-php/
链接地址: http://www.djcxy.com/p/64754.html