Для получения полного доступа
зарегистрируйтесь.
Snippet,  PHP

Memory efficient XML writer


The approach demonstrated is used in my Sitemap package to allow generating huge sitemaps consisting of hundreds thousangs of locations.

// we'll buffer data in memory in order not to write too often
$bufferSize = 1000;

$filePath = 'test.xml';

$writer = new XMLWriter();
// using memory for buffering
$writer->openMemory();


$writer->startDocument('1.0', 'UTF-8');
$writer->setIndent(true);
$writer->startElement('test');

while ($i < 10000) {                  
	if ($i % $bufferSize) {
		// if buffer is full, flush its contents into the file
		file_put_contents($filePath, $writer->flush(true), FILE_APPEND);
	}
	$writer->writeElement('item', $i);
}

$this->writer->endElement();

$this->writer->endDocument();

// write the rest of the buffer
file_put_contents($filePath, $writer->flush(true), FILE_APPEND);
  XML memory buffer

Автор


samdark
  • Reputation: 349
  • Snippets : 57
  • Revisions : 5
Подписаться

Чтобы увидеть комментарии, нужно быть участником сообщества

Регистрация