tags:

views:

396

answers:

6

I have a 50MB XML file. I want to convert it to a CSV file, but most methods I have found exhaust the server memory. Is there a good way to do this using a stream method such as XMLreader.

+2  A: 

the SAX-style expat-based parser is the most space-efficient option:

http://php.net/xml_parse

it will execute your $start_element_handler and $end_element_handler callbacks whenever an element tag is opened or closed, rather than keeping the entire document in memory.

but still, 50 MB is not a lot, maybe your provider can up the limit.

php_value memory_limit 100M

in .htaccess/httpd.conf, or set it in php.ini.

jspcal
The file is 50MB, but the process uses much much more than 50MB. I increased the memory limit quite a bit, but it still doesn't work
Brian
try xml_parse with those callbacks.. it will use very little RAM
jspcal
A: 

Have you tried to increase memory limit ? ini_set('memory_limit', '256M')

(That's a very bad solution btw)

OcuS
A: 

I don't know much about PHP API, but seems this class can help you: XML Parser

Basically you're looking for a parser based on events, like old SAX. This parser type will fire an event, or something similar. It'll be memory efficient, as it doesn't need to load your entire document into memory.

Rubens Farias
+3  A: 

You'd want to use XmlReader to parse the XML, as it works as an event based parser - Eg. it doesn't load everything into memory, but rather reads as it advances through the input file.

troelskn
A: 

If the XML file is rather simple and could avoid going through a full-fledged XML parser, and could instead be read line-by-line by PHP and export each line as it goes, that would save having the whole file in memory at once. What's the XML structure?

MidnightLightning
A: 

I've written this algorithm some time ago.. Feel free to give it a shot.

http://sites.google.com/site/soichih/q-a/xml-to-csv-converter

SoichiH