2

I am trying to parse XML to array in my custom module in Magento 2.2.7. Community version (free)

I am loading Helper\Data through Block element and this is "working".


This is my Helper\Data.php

<?php

namespace Vendor\Categories\Helper;
use Magento\Framework\Serialize\SerializerInterface;

class Data extends \Magento\Framework\App\Helper\AbstractHelper
{

   private $xml2;
   private $xml; 

  public function __construct(SerializerInterface $serializer) {
    $this->serializer = $serializer;
    $this->xml2 = dirname(file_get_contents('import/kategorije-za-import.xml'));
    $this->xml = $this->serializer->unserialize($this->serializer->serialize(simplexml_load_string($this->xml2)));

  } // end of construct

  public function testing(){
    var_dump($this->xml);
  }

This outputs error in

[2019-02-16 10:27:51] main.CRITICAL: Warning: simplexml_load_string(): Entity: line 1414: parser error : StartTag: invalid element name in /home/user/public_html/app/code/Vendor/Categories/Helper/Data.php on line 19 [] []

XML file location is: \home\user\public_html\import\kategorije-za-import.xml

My xml file is:

<?xml version='1.0' encoding='UTF-8'?>
<NewDataSet>
  <Table>
    <CategoryID>1</CategoryID>
    <CategoryName>Hardware</CategoryName>
    <ParentCategoryID>0</ParentCategoryID>
  </Table>
  <Table>
    <CategoryID>2</CategoryID>
    <CategoryName>Paketi i bundle</CategoryName>
    <ParentCategoryID>1</ParentCategoryID>
  </Table>
  <Table>
    <CategoryID>3</CategoryID>
    <CategoryName>Matične ploče</CategoryName>
    <ParentCategoryID>1</ParentCategoryID>
  </Table>
  <Table>
    <CategoryID>4</CategoryID>
    <CategoryName>Intel</CategoryName>
    <ParentCategoryID>3</ParentCategoryID>
  </Table>
</NewDataSet>

I have tested this as a script outside Magento and XML file loads and parses as array.

What to do? :)

7
  • There is a missing < sign at the first line. <?xml....
    – Marius
    Feb 16, 2019 at 10:50
  • Thx, I have missed it when copying. If only it was that simple :)
    – Oktarin
    Feb 16, 2019 at 12:01
  • <CategoryName>Matične ploče</CategoryName> -> problem is here
    – Sohel Rana
    Feb 16, 2019 at 12:24
  • @SohelRana can you please explain? I don't see it...
    – Oktarin
    Feb 16, 2019 at 12:35
  • can you try with \Magento\Framework\Xml\Parser instead of file_get_contents ?For reference just check the file Magento\Framework\Module\ModuleList\Loader:: load method
    – Haijerome
    Feb 16, 2019 at 15:59

1 Answer 1

0

In the code you have put into your question you pass the wrong content to the method simplexml_load_string. $this->xml2contains the ouput of dirname() which can't contain the content of your xml file as string (the input simplexml_load_string expects). This code should do what you expect:

public function __construct(SerializerInterface $serializer) {
   $this->serializer = $serializer;
   //get the root directory of your Magento installation (I assume your import directory has been created in the root directory. 
   //If not you have to change that part. You may also hardcode the path for testing
   $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
   $directoryList = $objectManager->get('\Magento\Framework\App\Filesystem\DirectoryList');
   $path = $directoryList->getPath(\Magento\Framework\App\Filesystem\DirectoryList::ROOT);
   $this->xml2 = file_get_contents($path .'/import/kategorije-za-import.xml');
   $this->xml = $this->serializer->unserialize($this->serializer->serialize(simplexml_load_string($this->xml2)));

} // end of construct

I hope that helps.

0

Your Answer

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

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