create xml file from mysql data to slowly
<p>I'd like to create a xml file from mysql data queries.</p>

<p>this part works well but it's very slowly around 15s to create a file with around 5000 lines.</p>

<p>On apache server, compression is activate.</p>

<p>See my code:</p>

<pre><code>    $recordset = mysql_query($query, $link);

$dom = new DOMDocument('1.0', 'UTF-8');
$rootNode= $dom-&gt;appendChild($dom-&gt;createElement('DatabaseDB'));

while($row_recordset = mysql_fetch_row($recordset))

{

    $record = new DomElement("p", "");
    $rootNode-&gt;appendChild($record);

    $record-&gt;SetAttribute("id", utf8_encode($row_recordset[0]));
    $record-&gt;SetAttribute("np", utf8_encode($row_recordset[1]));
    $record-&gt;SetAttribute("pr", utf8_encode($row_recordset[8]));
    $record-&gt;SetAttribute("d1", utf8_encode($row_recordset[2])." / ".utf8_encode($row_recordset[3]));
    $record-&gt;SetAttribute("dn", (datefr($row_recordset[4])));
    $record-&gt;SetAttribute("tel", utf8_encode($row_recordset[5]));

    $record-&gt;SetAttribute("st", utf8_encode($row_recordset[6])." - ".(datefr($row_recordset[7])));


}
mysql_free_result($recordset);
mysql_close();

print $dom-&gt;saveXML();
</code></pre>

<p>If you have an idea to load more quickly, I'm very happy. 
Thanks</p>

<p>According answer, I try that:</p>

<pre><code>    $writer = new XmlWriter();

$writer-&gt;setIndent(true);
$writer-&gt;openURI('php://output');
$writer-&gt;startDocument('1.0', 'utf-8');
$writer-&gt;startElement('DatabaseDB');
while($row_recordset = mysql_fetch_row($recordset))

{

    $writer-&gt;startElement('p');
    $writer-&gt;writeAttribute("id", utf8_encode($row_recordset[0]));
    $writer-&gt;writeAttribute("np", utf8_encode($row_recordset[1]));
    $writer-&gt;writeAttribute("pr", utf8_encode($row_recordset[8]));
    $writer-&gt;writeAttribute("d1", utf8_encode($row_recordset[2])." / ".utf8_encode($row_recordset[3]));
    $writer-&gt;writeAttribute("dn", (datefr($row_recordset[4])));
    $writer-&gt;writeAttribute("tel", utf8_encode($row_recordset[5]));
    $writer-&gt;writeAttribute("st", utf8_encode($row_recordset[6])." - ".(datefr($row_recordset[7])));

    $writer-&gt;endElement();
}
$writer-&gt;endElement();
$writer-&gt;flush();
</code></pre>

<p>But, with DomDocument, it's take about 9s to create file (5000 rows)  and with XmlWriter is arround 8s.</p>

<p>Do you have a new idea to optimize?
Thanks
So, the perform is not really better.</p>
