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/
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/
本文介绍如何使用PHP的XMLWriter类生成大型XML文件。通过两种方式:内存操作和URI输出,展示如何有效地管理资源并确保数据完整写入。每1000次迭代刷新一次XML数据到文件中。
4758

被折叠的 条评论
为什么被折叠?



