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.
<
sign at the first line.<?xml....