I need to read XML files about 1 GB in size. My XML:
<products>
<product>
<categoryName>Kable i konwertery AV</categoryName>
<brandName>Belkin</brandName>
<productCode>AV10176bt1M-BLK</productCode>
<productId>5616488</productId>
<productFullName>Kabel Belkin Kabel HDMI Ultra HD High Speed 1m-AV10176bt1M-BLK</productFullName>
<productEan>0745883767465</productEan>
<productEuroPriceNetto>59.71</productEuroPriceNetto>
<productFrontendPriceNetto>258.54</productFrontendPriceNetto>
<productFastestSupplierQuantity>23</productFastestSupplierQuantity>
<deliveryEstimatedDays>2</deliveryEstimatedDays>
</product>
<product>
<categoryName>Telewizory</categoryName>
<brandName>Sony</brandName>
<productCode>KDL32WD757SAEP</productCode>
<productId>1005662</productId>
<productFullName>Telewizor Sony KDL-32WD757 SAEP</productFullName>
<productEan></productEan>
<productEuroPriceNetto>412.33</productEuroPriceNetto>
<productFrontendPriceNetto>1785.38</productFrontendPriceNetto>
<productFastestSupplierQuantity>11</productFastestSupplierQuantity>
<deliveryEstimatedDays>6</deliveryEstimatedDays>
</product>
<product>
<categoryName>Kuchnie i akcesoria</categoryName>
<brandName>Brimarex</brandName>
<productCode>1566287</productCode>
<productId>885156</productId>
<productFullName>Brimarex Drewniane owoce, Kiwi - 1566287</productFullName>
<productEan></productEan>
<productEuroPriceNetto>0.7</productEuroPriceNetto>
<productFrontendPriceNetto>3.05</productFrontendPriceNetto>
<productFastestSupplierQuantity>7</productFastestSupplierQuantity>
<deliveryEstimatedDays>3</deliveryEstimatedDays>
</product>
</products>
I use XML reader.
$reader = new XMLReader();
$reader->open($url);
$count = 0;
while($reader->read()) {
if($reader->nodeType == XMLReader::ELEMENT)
$nodeName = $reader->name;
if(($reader->nodeType == XMLReader::TEXT || $reader->nodeType == XMLReader::CDATA)) {
if ($nodeName == 'categoryName') $categoryName = $reader->value;
if ($nodeName == 'brandName') $brandName = $reader->value;
if ($nodeName == 'productCode') $productCode = $reader->value;
if ($nodeName == 'productId') $productId = $reader->value;
if ($nodeName == 'productFullName') $productFullName = $reader->value;
if ($nodeName == 'productEan') $productEan = $reader->value;
if ($nodeName == 'productEuroPriceNetto') $productEuroPriceNetto = $reader->value;
if ($nodeName == 'productFastestSupplierQuantity') $productFastestSupplierQuantity = $reader->value;
if ($nodeName == 'deliveryEstimatedDays') $deliveryEstimatedDays = $reader->value;
}
if($reader->nodeType == XMLReader::END_ELEMENT && $reader->name == 'product') {
$count++;
}
}
$reader->close();
All is working fine except one problem... When some value is missing, for example <productEan></productEan>
in output I am getting a value from the previous, not empty tag till another tag which is not empty.
For example, if previous node is like in example <productEan>0745883767465</productEan>
and another two <productEan></productEan>
are empty in output array I getting same value, 0745883767465
.
What is the right way to solve this problem? Or maybe some one have working solution...