2

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 ?

1
  • please check my solution and let me know if any issue. Commented Feb 12, 2019 at 5:30

3 Answers 3

2

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'];
                }
         }
4
  • my code is stuck at line : $this->parser->load. i have not getting false or true value in xml object Commented Feb 8, 2018 at 8:56
  • issue only getting while reading large file around 184MB xml file size Commented Feb 8, 2018 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 . Commented Feb 8, 2018 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) Commented Feb 8, 2018 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.

0

Try This Code

protected $parser;

public function __construct(
    \Magento\Framework\Xml\Parser $parser
) {
    $this->parser = $parser;
}

public function getXMLArrayData()
{
    $filePath = "/var/www/html/mydemosite/pub/media/sitemap/filename.xml";
    $parsedArray = $this->parser->load($filePath)->xmlToArray();

    return $parsedArray;

    //$json = json_encode($parsedArray);
    //$array = json_decode($json, true);
}

I Hope This Helps You.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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