I have a feed with several products in it, but the fields are not known. I want to display a "tree structure" from the file with PHP.
THe XML looks like following:
<products>
<product id="1">
<title>Title of product 1</title>
<weight measure="kg">4000</weight>
<specs>
<spec name="spec1">
<value>value spec 1</value>
<spec name="spec2">
<value>value spec 2</value>
</specs>
</product>
<product id="2">
<title>Title of product 2</title>
<weight measure="kg">10000</weight>
<specs>
<spec name="spec1">
<value>value spec 1</value>
<spec name="spec2">
<value>value spec 2</value>
<spec name="spec3">
<value>value spec 3</value>
</specs>
</product>
</products>
I'd like my PHP to display something like this so that I can insert these values in the database:
Product: 1
title: Title of product 1
weight: 4000
spec1: value spec 1
spec2: value spec 2
Product: 2
title: Title of product 2
weight: 10000
spec1: value spec 1
spec2: value spec 2
spec3: value spec 3
As mentioned, the feeds are differently every time, with different fields. Thats making it more difficult to me..
$xml = simplexml_load_file($feedURL);
foreach($xml->product as $products) {
echo '>.'.$products[ID].'<br>';
foreach($products->children() as $properties){
echo $properties[name].': '.$properties->value.'<br>';
foreach($properties->children() as $aspects){
echo '--->'.$aspects[name].': '.$aspects->value.'<br>';
}
}
}
It does not work at this moment. How could I fix this?