XML file contains for about 10 000 products with different 'cat_top' attribute. I need to parse only products that belong to a category "PC".
<product id="6055551" cat="Motherboards" cat_top="PC">
</product>
<product id="6666691" cat="Motherboards" cat_top="PC">
</product>
<product id="6044391" cat="Motherboards" cat_top="PC">
</product>
<product id="6041391" cat="iPad" cat_top="Apple">
</product>
<product id="6041391" cat="iPhone" cat_top="Apple">
</product>
<product id="6423391" cat="iPad" cat_top="Apple">
</product>
<product id="6067391" cat="iPhone" cat_top="Apple">
</product>
This is my code:
<?php
if (file_exists('xml_im.xml')) {
$xml = simplexml_load_file('xml_im.xml');
} else {
exit('Не удалось открыть файл xml_im.xml.');
}
foreach($xml->product as $product) {
$name = $product['id'];
$cat = $product['cat'];
echo $name, " " , $cat , " ";
}
?>