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– Aasim Goriya2019年02月12日 05:30:15 +00:00Commented Feb 12, 2019 at 5:30
3 Answers 3
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 objectDhaval Vaghela– Dhaval Vaghela2018年02月08日 08:56:06 +00:00Commented Feb 8, 2018 at 8:56
-
issue only getting while reading large file around 184MB xml file sizeDhaval Vaghela– Dhaval Vaghela2018年02月08日 08:58:25 +00:00Commented 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 .Shashank Gupta– Shashank Gupta2018年02月08日 09:31:26 +00:00Commented 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)Shashank Gupta– Shashank Gupta2018年02月08日 09:36:58 +00:00Commented Feb 8, 2018 at 9:36
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.
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.
Explore related questions
See similar questions with these tags.