php - How do I parse this large xml file with XMLreader? -
i have xml file 200 mb in size have decided utilize xmlreader extract info need.
here snippet of xml in file:
<product> <manufacturer>blue widgets</manufacturer> <price>6.79</price> <condition>new</condition> </product> <product> <manufacturer>green widgets</manufacturer> <price>9.99</price> <condition>new</condition> </product> <product> <manufacturer>black widgets</manufacturer> <price>3.29</price> <condition>new</condition> </product> <product> <manufacturer>blue widgets</manufacturer> <price>14.99</price> <condition>new</condition> </product>
of thousands of records in big file, need info manufacturer "blue widgets", having problem figuring out how isolate manufacturer:
$reader = new xmlreader(); $reader->open('test.xml'); $products = array(); while($reader->read()){ if($reader->nodetype == xmlreader::element && $reader->localname == 'manufacturer'){ $reader->read(); if($reader->value == 'blue widgets'){ //now know manufacturer right, how advance next cost node within product element? if($reader->nodetype == xmlreader::element && $reader->localname == 'price'){ $reader->read(); $products['price'] = $reader->value } } } }
you can utilize xmlreader::next
method. using example:
... $reader->read(); if ($reader->value == 'blue widgets') { $reader->next('price'); // advances cursor cost node } ...
documentation: http://php.net/manual/en/xmlreader.next.php
php xml xmlreader
No comments:
Post a Comment