Working with large XML files in PHP

There are many ways to manage big XML files in php. When I say large, is really large, 10, 20, 30 mebabytes. If we were open a file like this on a common server, we certainly will have a time-out error. This is because we usually use SimpleXML functions. This extensions is, in fact, a “Tree-based parser”, just like “DOM Parser”. They work great in small-medium XML files (~1MB). They put the whole content on memory, and then, parse it. But when we face a big content, the only option is the “Stream-based parsers”. These are more efficiently and faster, because it read the file on demand, and dont crush your servers memory.

Amongst the stream-based parsers, we have SAX and XMLReader. Ill show you how to read a big XML with XMLReader, because its easier and faster comparing to SAX, as you can se here.

The XMLReader is an extension enabled by default on PHP 5.1 and earlier. Born from XmlTextReader API (C#) and its based om libxml2 library. Before that, the XMLReader extension was only available on PECL. XMLReader supports namespaces and validations, including DTD and Relax NG (REgular LAnguage for XML Next Generation).

Well, lets to the code. Heres me XML example:

PHP code:

See, in determinate point I transform the actual block of data (the users tag) into a SimpleXML object, making the manipulation extremely easy. This way, you can easily work with big XML with no harm to the servers memory and no decrease on speed. My next move: try to optimize this script and maybe create a CodeIgniters library. That certainly will be a nice addon to developers, I think. Of course any help will be very welcome!

Did you like this? Share it:

So, what do you think ?