Load XML element by element, to save memory in PHP
I need to read an XML file on a low memory system (don't have access to increase it). I've tried:
$xmlString = file_get_contents($file);
$doc= new DOMDocument();
$doc->loadXML($xmlString);
and
$doc= new DOMDocument();
$doc->load($file);
Both give me memory errors.
Was wondering if there is a way to read the XML sequentially node by node, so only 1 node is in memory at any given time. I don't care how long this takes as it's a nightly batch process.
The 开发者_运维知识库structure of the XML is very simple:
<InventoryItem>
<SKU>125244</SKU>
<Quantity>137196</Quantity>
<Status>Active</Status>
</InventoryItem>
repeated may times.
Maybe treating it as text?
Much thanks
You need a pull parser like XMLReader.
You want to use SAX parsing (using the PHP functions, or XMLReader, as mentioned in another answer).
精彩评论