I have an XML document like this, it's > 400 MB file.
My issue is that I cannot get XMLReader to not run into memory limit, have a 512 Mb PHP 7.2 server.
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GetModifiedResponse xmlns="http://host.com">
<ProductList>
<UpdatedProducts>
<ProductId>1</ProductId>
<ProductId>2</ProductId>
<ProductId>3</ProductId>
<ProductId>4</ProductId>
</UpdatedProducts>
<RemovedProducts>
<ProductId>5</ProductId>
<ProductId>6</ProductId>
<ProductId>7</ProductId>
<ProductId>8</ProductId>
</RemovedProducts>
</ProductList>
..
This is kinda my script, and the issue here is that the whole "UpdatedProducts" is loaded and max the ram out. And need a similar for RemovedProducts, both need to be in the loop, how to solve the problem - if possible w.o. put more ram in the server (or memory_limit(-1))?
while ($xml->name == 'UpdatedProducts') {
$elm = new \SimpleXMLElement($xml->readOuterXml());
foreach ($elm->ProductId as $product) {
$this->saveToDb($product);
}
$xml->next('UpdatedProducts');
}
Update:
the code is right now
$xml = new \XMLReader();
$xml->open(__DIR__ . '/../../var/tmp/out.xml');
while ($xml->read()) {
while ($xml->name == 'UpdatedProducts') {
while ($xml->read() && $xml->name != 'ProductId');
while ($xml->name == 'ProductId') {
$this->saveToDb($xml->readInnerXml(), 'update');
$xml->next('ProductId');
}
$xml->next('UpdatedProducts');
}
while ($xml->name == 'RemovedProducts') {
while ($xml->read() && $xml->name != 'ProductId');
while ($xml->name == 'ProductId') {
$this->saveToDb($xml->readInnerXml(), 'remove');
$xml->next('ProductId');
}
$xml->next('RemovedProducts');
}
}
memory_limit(-1)?