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.
the SAX-style expat-based parser is the most space-efficient option:
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.
Have you tried to increase memory limit ? ini_set('memory_limit', '256M')
(That's a very bad solution btw)
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.
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?
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