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 ?

Aasim Goriya
5,4622 gold badges30 silver badges54 bronze badges
asked Feb 8, 2018 at 8:13
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'];
 }
 }
answered Feb 8, 2018 at 8:22
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.

answered Jan 7, 2019 at 16:44
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.

answered Jun 15, 2021 at 9:25

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.