PHP XML Writer

PHP XML Writer

PHP XML writer memory sample
<?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-memory.xml', $xmlWriter->flush(true), FILE_APPEND);
    }
}
// Final flush to make sure we haven't missed anything
file_put_contents('example-memory.xml', $xmlWriter->flush(true), FILE_APPEND);

?>

PHP XML Writer file Sample
<?php
$xmlWriter = new XMLWriter();
$xmlWriter->openURI('example-uri.xml');
$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) {
        $xmlWriter->flush();
    }
}
// Final flush to make sure we haven't missed anything
$xmlWriter->flush();

?>

Execute the command, we can see the performance
> php xmlwriter-uri.php

References:
http://codeinthehole.com/writing/creating-large-xml-files-with-php/

猜你喜欢

转载自sillycat.iteye.com/blog/2361321