2

I have a xml file that has more than 5000 products (items). It's really a big file and the execution of parsing that file is taking more than 40 seconds.

My php code :

ini_set('max_execution_time', 300);
$xml = simplexml_load_file("wwww.example.com");
print_r($xml);

Is there any solution for this?? for example loading just a part of file?

Vahid Najafi
  • 4,654
  • 11
  • 43
  • 88
  • Caching might be a solution or handling in the background (e.g. with a gearman server). – Jan Feb 28 '16 at 11:51
  • @Jan Do you have any tutorial source? Thanks. – Vahid Najafi Feb 28 '16 at 11:54
  • 1
    http://gearman.org/ or http://queues.io/ Alternatively [look at this question here on SO.](http://stackoverflow.com/questions/3048583/what-is-the-fastest-xml-parser-in-php) – Jan Feb 28 '16 at 11:56

2 Answers2

1

Try to call the simplexml_load_file function with LIBXML_COMPACT flag:

$xml = simplexml_load_file("wwww.example.com",'SimpleXMLElement',LIBXML_COMPACT);
das-g
  • 9,718
  • 4
  • 38
  • 80
Tom Top
  • 11
  • 1
  • I searched for `LIBXML_COMPACT` and found this: Activate nodes allocation optimization (may speed up application). But not a big difference. `SAX` parse is a good idea as Prince Rajput mentioned. Any way, thanks a lot. – Vahid Najafi Feb 28 '16 at 15:33
  • I did some looking up what this does and from looking at the libxml2 docs it appears that it can slightly speed up reading in some cases, but as a result you cannot make any modifications to the DOM later. So, fine if all you want to do is read, and not make changes. I find it curious the PHP docs didn't mention that restriction, so maybe in PHP that isn't the case... – thomasrutter Jul 06 '16 at 04:03
1

XMLReader is another solution, like SAX (or expat), that allows you to read the XML file incrementally and it should be easier to work with than the former.

It can make things substantially faster if you don't need to parse the whole file and can stop when you've found what you were looking for.

If you still need to parse the whole file with it, it won't offer a speedup.

thomasrutter
  • 114,488
  • 30
  • 148
  • 167