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 ?
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 ?
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'];
}
}
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.