od kilku dni szukam, czytam i testuję. Mam do przetworzenia katalog, który dostałem w pliku XML (cały waży ok. 5 mb, zawiera kilkaset produktów opisanych w 110 tys. linii). Obecnie jestem na etapie SimpleXML, ale nie wiem, czy idę w dobrą stronę i stąd ten post.
Skrypt wyszukuje produkt według jego unikalnego ID, następnie wyciąga potrzebne dane produktu z odpowiednich elementów (identyfikowanych według ich atrybutu), wyświetla to w przeglądarce (docelowo ma zapisywać do bazy SQL).
Poniżej zamieszczam mój roboczy skrypt:
<?php $katalog = simplexml_load_file('przyklad.xml'); $szukaneid = 1071370015; //szuka produktu po ID foreach($katalog->country_product as $produkt){ if($produkt->product->attributes()->id == $szukaneid){ foreach($produkt->contents->content as $wyswietl1){ foreach($wyswietl1->nomenclatura as $wyswietl2){ if($wyswietl2->attributes()->key == "Benefit"){ } } } //Sklad foreach($produkt->{'ingredient-amounts'}->ingredient_amount as $skladnik1){ if($skladnik1->ingredient->ingredient_section->attributes()->name_key == "analysis_table"){ } } //Mineraly foreach($produkt->{'ingredient-amounts'}->ingredient_amount as $skladnik2){ if($skladnik2->ingredient->ingredient_section->attributes()->name_key == "minerals"){ } } //WITAMINY foreach($produkt->{'ingredient-amounts'}->ingredient_amount as $skladnik3){ if($skladnik3->ingredient->ingredient_section->attributes()->name_key == "vitamins"){ } } } } ?>
Przykładowy produkt z katalogu XML:
<?xml version="1.0" encoding="UTF-8"?> <country_products xsi:noNamespaceSchemaLocation="ab_files/schema.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <country_product id="868484849"> <name>Pasta KL3BL731</name> <product id="1071370015"> <name>Pasta KL3BL731</name> </product> <!-- contents --> <contents> <content type="TextContent"> <nomenclatura key="MetaTagsDescription" id="2028553811"> <name>Meta Tags Description</name> </nomenclatura> <order_nr>2</order_nr> <text> <![CDATA[Some meta tags]]> </text> </content> <content type="ImageParagraphContent"> <nomenclatura key="Benefit" id="1102795634"> <name>Benefit</name> </nomenclatura> <order_nr>300</order_nr> <alt> <![CDATA[Vit support]]> </alt> <title> <![CDATA[]]> </title> <headline> <![CDATA[VITAMINS C, E]]> </headline> <filename>html/product_images/benefit_4120.png</filename> <text> <![CDATA[<p>Protect skin cells from UV damage </p>]]> </text> </content> </contents> <!-- analysis_table --> <ingredient-amounts type="array"> <ingredient_amount> <amount type="float">0.06</amount> <unit name_key="percent" id="10"> <name>%</name> </unit> <ingredient name_key="arachidonic_acid" id="3"> <ingredient_section name_key="analysis_table" id="865537301"> <name>Analysis</name> </ingredient_section> <name>Arachidonic acid</name> </ingredient> </ingredient_amount> <ingredient_amount> <amount type="float">0.277</amount> <unit name_key="mg_per_kg" id="30"> <name>mg/kg</name> </unit> <ingredient name_key="biotin" id="58"> <ingredient_section name_key="analysis_table" id="865537301"> <name>Analysis</name> </ingredient_section> <name>Biotine</name> </ingredient> </ingredient_amount> <ingredient_amount> <amount type="float">0.082</amount> <unit name_key="percent" id="10"> <name>%</name> </unit> <ingredient name_key="calcium" id="7"> <ingredient_section name_key="analysis_table" id="865537301"> <name>Analysis</name> </ingredient_section> <name>Calcium</name> </ingredient> </ingredient_amount> <ingredient_amount> <amount type="float">0.46</amount> <unit name_key="percent" id="10"> <name>%</name> </unit> <ingredient name_key="chlore" id="95"> <ingredient_section name_key="minerals" id="1"> <name>Minerals</name> </ingredient_section> <name>Chlore</name> </ingredient> </ingredient_amount> <ingredient_amount> <amount type="float">200.0</amount> <unit name_key="mg_per_kg" id="30"> <name>mg/kg</name> </unit> <ingredient name_key="choline" id="31"> <ingredient_section name_key="vitamins" id="2"> <name>Vit</name> </ingredient_section> <name>Choline</name> </ingredient> </ingredient_amount> <ingredient_amount> <amount type="float">1.5</amount> <unit name_key="mg_per_kg" id="30"> <name>mg/kg</name> </unit> <ingredient name_key="copper" id="89"> <ingredient_section name_key="minerals" id="1"> <name>Minerals</name> </ingredient_section> <name>Copper</name> </ingredient> </ingredient_amount> </ingredient-amounts> </country_product> </country_products>
Czy SimpleXML to dobre wyjście w tej sytuacji?
Obawiam się tych wszystkich pętli, przy zapisie kilkuset produktów pewnie będzie ciężko. Jak inaczej mogę dotrzeć do wartości zagnieżdżonej w danym elemencie wyszukując go według atrubutu?
Z góry dziękuję za pomoc,
Pozdrawiam