2

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

Henry
  • 1,374
  • 2
  • 14
  • 24

2 Answers2

3

You need a pull parser like XMLReader.

Álvaro González
  • 142,137
  • 41
  • 261
  • 360
  • This did the trick, and implementing it took 5 minutes using this example: http://stackoverflow.com/questions/1835177/how-to-use-xmlreader-in-php Muchas Gracias! – Henry Jun 29 '11 at 20:07
0

You want to use SAX parsing (using the PHP functions, or XMLReader, as mentioned in another answer).

brianary
  • 8,996
  • 2
  • 35
  • 29