1

I have product file in XML format. I just need to read this file and import those data.

I have already use simplexml_load_file(file path) but it will work only small size of file. Is there any other way to read this large XML file ?

  • please check my solution and let me know if any issue. – Aasim Goriya Feb 12 at 5:30
1

You can try this way :

 namespace Vendor\ModuleName\Model;  
      class ParseXmlClass 
         {
                /**
                 * @var \Magento\Framework\Module\Dir\Reader
                 */
                protected $moduleDirReader;

                /**
                 * @var \Magento\Framework\Xml\Parser
                 */
                private $parser;

                    ...

                public function getValue() 
                {
                    $filePath = $this->moduleDirReader->getModuleDir('etc', 'ModuleName') 
                       . '/yourxml.xml'
                    $parsedArray = $this->parser->load($filePath)->xmlToArray();
                    return $parsedArray['xmlNodeName'];
                }
         }
  • my code is stuck at line : $this->parser->load. i have not getting false or true value in xml object – Camit1dk Feb 8 '18 at 8:56
  • issue only getting while reading large file around 184MB xml file size – Camit1dk Feb 8 '18 at 8:58
  • If you are not getting true and false from code and also there is no error it mean's the script is running , So I would recommend to do following : 1. Increase max execution time and and max post file size , by default it set to 2 MB increase to 512 MB in php.ini 2. If possible increase you server CPU or RAM it will help you for faster execution . Hope solution will work for you . – Shashank Gupta Feb 8 '18 at 9:31
  • Find following value in php.ini post_max_size = 512M upload_max_filesize = 256M memory_limit = 2048M Then run following command sudo service apache2 restart (if you are using apache2) sudo service httpd restart (if you are using nginx) – Shashank Gupta Feb 8 '18 at 9:36
0

Totally shooting form the hip here, but have you tried PHP's XMLReader or DOMDocument instead of SimpleXML? SimpleXML loads the full document to memory, while the other two do not.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.