0

I think I pretty much asked it all in the headline.

The issue I'm having is that I'm trying to parse an XML file with SimpleXML that SOMETIMES fails to read the xml but sometimes is successful. I can only think it's down to the script running out of memory.

I have 200mb XML product feed, but it's only nested to around 4 layers maximum. Is it okay to use SimpleXML for this? Is it just a case of increasing my memory limit in php.ini (I can't do it to test it yet as my server imposes maximums)?

Thanks!

Matt Wills
  • 676
  • 6
  • 11
  • Just discussed a case like this, the man had an HTML that he loads, even if the file is only 350mb like he said, PHP needed more memory to parse it to an object, I think you're having the same issue : https://stackoverflow.com/questions/46662542/cant-load-too-big-html-file-when-grab/46662624#46662624 – teeyo Oct 10 '17 at 15:32
  • Thanks! So just increasing memory limit in php ini should solve and I can still parse with SimpleXML? I’ve just written all my logic for SimpleXML now and it would be a huge hassle to have to switch to another XML parser because the file is too large. – Matt Wills Oct 10 '17 at 16:12
  • I guess the point is even if my memory limit is set higher than the file size, PHP still uses a load of memory to write the XML to an object and parse it so it can still cause an error? – Matt Wills Oct 10 '17 at 16:13
  • 1
    It may be worth looking into XML Reader, https://stackoverflow.com/questions/1835177/how-to-use-xmlreader-in-php shows a simple example. Why worry about just working - wait till the file grows to 400mb, then 600mb. – Nigel Ren Oct 10 '17 at 17:48
  • That’s what I was trying to avoid though cos I’ve already written the logic! And I can be sure the file won’t grow, it’s consistently 200mb :) – Matt Wills Oct 10 '17 at 18:06
  • The example I pointed to gives an example of XML reader and SimpleXML to process each segment. This allows the SimpleXML code to still work. – Nigel Ren Oct 10 '17 at 18:40
  • You are spending way too much time looking for a solution rather than just writing the XmlReader code. See my solution at following posting : https://stackoverflow.com/questions/38674351/reading-xml-file-with-xmlreader-reads-only-the-first-element – jdweng Oct 10 '17 at 19:49
  • Cheers guys, I think you’re right, need to just commit to using XML reader. I guess that was really my question though, like is the issue down to the file being too large for SimpleXML or should I be looking for errors elsewhere – Matt Wills Oct 10 '17 at 20:21

1 Answers1

0

The maximum file size that PHP can handle and the maximum file size that your system can handle is two different things. You have to adjust how much memory PHP can access and SimpleXML is fine to use.

  • So theoretically, if memory limit is set high enough in php.ini, SimpleXML should be able to handle any file size? We aren’t hugely bothered about performance since it’ll be run as a cron task – Matt Wills Oct 10 '17 at 16:09
  • 1
    Yes, I import XML-files that are 2gb with SimpleXML daily, that works just fine! So if your files are 200mb, I would not worry. –  Oct 11 '17 at 08:54
  • hmm, it's strange, I just keep getting 'failed loading XML' and no error message. ARGH. I'm gonna try another parser – Matt Wills Oct 19 '17 at 18:48