Reputation: 1442
Hello I have an xml feed which has many products and each product can have mutiple skus. I'm trying to loop over that xml and then if I find a match for the EAN code then I'm trying to output the matched skus as a seperate xml file.
The code I have so far is below, please can you point out where I am going wrong as it's not quite working yet and it's outputting an empty response.
$ean = $_GET["ean"];
$trialXML = simplexml_load_file('trial.xml');
//function to lookup an EAN
function lookupEAN($ean,$xml) {
//loop over products
foreach($xml->products->product as $product) {
//loop over individual skus
foreach($product->sku as $sku) {
if ($sku->ean == $ean) {
$skuData .= $sku;
}
}
}
return $skuData;
}
//lookup EAN
$response = lookupEAN($ean,$trialXML);
Header('Content-type: text/xml');
print_r($response);
Thi is an example product from the xml:
<product>
<id>11077810</id>
<name>Cooke & Lewis Flow Soap Dispenser</name>
<categoryId>9372170</categoryId>
<features/>
<url>http://www.diy.com/diy/jsp/bq/nav.jsp?action=detail&fh_secondid=11077810&fh_location=//catalog01/en_GB/categories<{9372014}/categories<{9372035}/categories<{9372170}/specificationsProductType=bathroom_fittings/specificationsSpecificProductType=soap_dishes___dispensers</url>
<productHierarchy>
<productHierarchyElement>Rooms</productHierarchyElement>
<productHierarchyElement>Bathrooms</productHierarchyElement>
<productHierarchyElement>Bathroom Accessories</productHierarchyElement>
<productHierarchyElement>Bathroom Fittings</productHierarchyElement>
<productHierarchyElement>Soap Dishes & Dispensers</productHierarchyElement>
</productHierarchy>
<averageRating>0.0</averageRating>
<numberOfReviews>0</numberOfReviews>
<sku>
<id>11568516</id>
<name>Cooke & Lewis Flow Soap Dispenser</name>
<description>From the tradtional to the downright modern, we have accessories to suit the style of your home. Choose from the classic bronze effect through to the contemporary chrome to add a finishing touch to your bathroom. Glass and wood enhance some of our bathroom ranges and some have matching toilet seats.</description>
<ean>0000005286911</ean>
<price>29.98</price>
<wasPrice/>
<deliveryBand>Next day</deliveryBand>
<deliveryCost>5.0</deliveryCost>
<deliveryTime>Collect at Store</deliveryTime>
<deliveryLeadTime>0</deliveryLeadTime>
<stockAvailability>1</stockAvailability>
<channel>In Store Only</channel>
<buyerCats>
<catLevel0>BATHROOMS</catLevel0>
<catLevel1>WALL MOUNTED ACCESSORIES</catLevel1>
<catLevel2>DECO</catLevel2>
</buyerCats>
<affiliateCats>
<affiliateCat0>Home & Garden</affiliateCat0>
</affiliateCats>
<categoryHierarchy>Bathroom Fittings</categoryHierarchy>
<imageUrl>http://s7g1.scene7.com/is/image/BandQ/0000005286911_001c_v001_zp</imageUrl>
<thumbnailUrl>http://s7g1.scene7.com/is/image/BandQ/0000005286911_001c_v001_zp?$75x75_generic$=</thumbnailUrl>
<facets>
<facet name="styleStyle">Flow</facet>
<facet name="ecoRecycle">false</facet>
<facet name="specificationsSpecificProductType">Soap Dishes & Dispensers</facet>
<facet name="ecoGrowFoods">false</facet>
<facet name="ecoSavesEnergy">false</facet>
<facet name="ecoNurtureNature">false</facet>
<facet name="ecoDLME">false</facet>
<facet name="specificationsProductType">Bathroom Fittings</facet>
<facet name="featuresBrand">Cooke & Lewis</facet>
<facet name="ecoHealthyHomes">false</facet>
<facet name="ecoSavesWater">false</facet>
<facet name="featuresColour">Chrome</facet>
</facets>
<relatedItems>
<relatedItem>0000005287512</relatedItem>
<relatedItem>0000005287673</relatedItem>
<relatedItem>0000005287673</relatedItem>
</relatedItems>
<optionalTags>
<optionalTag>ROOMS15</optionalTag>
</optionalTags>
</sku>
</product>
Upvotes: 0
Views: 88
Reputation: 6625
use simplexml' with
xpathto select the
`node you want - in 3 lines of code:
$xml = simplexml_load_string($x); // assume XML in $x
$sku = $xml->xpath("//sku[ean='$ean']")[0]; // select the 1st <sku> node with <ean> = $ean
echo htmlentities($sku->asXML()); // echo or save it as XML
see it working: http://codepad.viper-7.com/F1FR2f
PHP < 5.4, e.g.:
$sku = $xml->xpath("//sku[ean='$ean']");
$sku = $sku[0];
Upvotes: 1