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 ?
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityI 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.