PHP to XML creatin

HI
I have to create a huge XML file from a millions records table using php as per some xsd.

When I create 60000 records XML file it gets the size of 2Gb. WIth 2 milion record the XML file is going to be huge but I want to create a single XML file.

I know that dom document won’t work at all. So, I also tried XMLwriter but it also gives me an error as “out of memory” excatly after 2GB creation of XML file. I do not know if XMLWriter has any creteria of memory but if it has what is the other option of creating huge file which are more than 2GB.

My code snippets looks like

[php]$i =0;

//some query sql statment which return result in $result

$writer->startElement(‘persons’);

while($row = mysql_fetch_row($result))
{

$writer->startElement(‘person’);
$writer->writeElement(‘UniqueIdentifier’,$row[0]);
$writer->writeElement(‘Name’,$row[1]);

$writer->startElement('geos');
	$writer->writeElement('Latitude',$row[4]);
	$writer->writeElement('Longitude',$row[5]);
$writer->endElement();

$writer->endElement();

if($i == 1000)
{
	file_put_contents('new_Xml_CTFS_row.xml', $writer->flush(true), FILE_APPEND);
	$i = 0;
}

}

$writer->endElement();

file_put_contents(‘new_Xml_CTFS_row.xml’, $writer->flush(true), FILE_APPEND);[/php]

Please let me know what is the other option to do this or how can I create huge XML files more than 2GB in php
It will be great if I can get the answer fast. Its kind of urgent
Thanks,
s

The method of writing the file in batches - what you have done - seems to be the only thing I can think of. Does the database have any kind of exporting feature? Or can you use something (e.g. PhpMyAdmin for MySQL) that would allow you to export into XML format?

Even tho the idea is what you want, remember this is 60,000 records, you still will encounter a memory limit. as each iteration creates a new node (person), so you may have to clear out the node once it has been written out. Reducing the amount of memory to a single node per iteration.

Sponsor our Newsletter | Privacy Policy | Terms of Service