PHP XML Writer

PHPXMLWriter

PHPXMLwritermemorysample

<?php

$xmlWriter=newXMLWriter();

$xmlWriter->openMemory();

$xmlWriter->startDocument('1.0','UTF-8');

for($i=0;$i<=10000000;++$i){

$xmlWriter->startElement('message');

$xmlWriter->writeElement('content','Examplecontent');

$xmlWriter->endElement();

//FlushXMLinmemorytofileevery1000iterations

if(0==$i%1000){

file_put_contents('example-memory.xml',$xmlWriter->flush(true),FILE_APPEND);

}

}

//Finalflushtomakesurewehaven'tmissedanything

file_put_contents('example-memory.xml',$xmlWriter->flush(true),FILE_APPEND);

?>

PHPXMLWriterfileSample

<?php

$xmlWriter=newXMLWriter();

$xmlWriter->openURI('example-uri.xml');

$xmlWriter->startDocument('1.0','UTF-8');

for($i=0;$i<=10000000;++$i){

$xmlWriter->startElement('message');

$xmlWriter->writeElement('content','Examplecontent');

$xmlWriter->endElement();

//FlushXMLinmemorytofileevery1000iterations

if(0==$i%1000){

$xmlWriter->flush();

}

}

//Finalflushtomakesurewehaven'tmissedanything

$xmlWriter->flush();

?>

Executethecommand,wecanseetheperformance

>phpxmlwriter-uri.php

References:

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

相关推荐