Quick tip to minimize memory when generating documents with DOM.
Rather than using
$xmlStr = DOMDocument->saveXML();
echo $xmlStr;
to dump a large DOM to the output buffer, use a PHP output stream, as in
DOMDocument->save('php://output');
A lot of memory will be saved when generating large DOMs.