I am trying to read an XML file in a Symfony project, to send the data to the front end.
As the XML file is huge, I am using a combination of XMLReader and SimpleXML (as suggested in this thread: How to use XMLReader in PHP?).
Here is my code for the XML Reader:
class XMLReader {
private $xmlFile;
private $reader;
public function __construct($xmlFile = null)
{
$this->xmlFile = $xmlFile;
}
public function initReader()
{
$this->reader = new \XMLReader();
$this->reader->open($this->xmlFile);
}
public function getData()
{
$products = array();
$index = 0;
while ($this->reader->read()) {
while ($this->reader->name === 'product') {
$node = new \SimpleXMLElement($this->reader->readOuterXML());
array_push($products, $node);
$index++;
if ($index < 20)
$this->reader->next();
else
break 2;
}
}
return $products;
}
My aim is to send the data little by little, as they will be displayed in a table with pagination. Which means the first time, it will send 20 results, and then when we click page 2, it will request the next 20 results.
Is there a way to do that?