How do I parse this large xml file with XMLreader?

477 views Asked by At

I have an xml file that is nearly 200 MB in size so I have decided to use XMLreader to extract the information I need.

Here is a snippet of the xml in this 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 the thousands of records in this large file, I only need the information from those where the manufacturer is "Blue Widgets", but I am having trouble figuring out how to isolate only that 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 that we know the manufacturer is right, how do I advance to the next price node within this product element?
          if($reader->nodeType == XMLREADER::ELEMENT && $reader->localName == 'price'){
              $reader->read();

              $products['price'] = $reader->value
         }
       }
     }
   }
1

There are 1 answers

0
Kai Schaller On BEST ANSWER

You can use the XMLReader::next method. Using your example:

...
$reader->read();
if ($reader->value == 'Blue Widgets') {
    $reader->next('price'); // Advances cursor to the price node
}
...

Documentation: http://php.net/manual/en/xmlreader.next.php