I have xml files like this:
<?xml version="1.0" encoding="utf-8"?>
<products>
<product ID="12345">
<name>Sunny Beach Resort</name>
<price currency="EUR">439.00</price>
<URL>https://www.example.com/</URL>
<images>
<image>https://www.example.com/images/image.jpg</image>
</images>
<properties>
<property name="country">
<value>Spanje</value>
</property>
<property name="region">
<value>Gran Canaria</value>
</property>
<property name="cityURL">
<value>https://www.example.com/</value>
</property>
<property name="usp">
<value>Adults only 16+</value>
<value>Een rustpunt op een centrale plek</value>
<value>Lichte kamers met ruimtelijke sfeer</value>
</property>
<property name="surrounding">
<value>afstand tot strand circa 1 kilometer (zandstrand)</value>
<value>afstand tot centrum: circa 1 kilometer</value>
<value>afstand tot Barstreet circa 500 meter</value>
<value>afstand tot luchthaven circa 30 kilometer</value>
</property>
<property name="serviceType">
<value>Logies ontbijt</value>
</property>
</properties>
<variations>
<variation>
<property name="roomType">
<value>2-persoonskamer luxe type voor alleengebruik</value>
</property>
<property name="roomOccupation">
<value>geschikt voor 1 persoon</value>
</property>
</variation>
<variation>
<property name="roomType">
<value>2-persoonskamer luxe type standaard</value>
</property>
<property name="roomOccupation">
<value>geschikt voor 2 tot 3 personen</value>
</property>
</variation>
</variations>
</product>
Where the name, url and image are very reliable values to extract in php. But when it comes to the entries they put within properties or even variations, it becomes very unreliable. Because sometimes they change the order of the data entries. Or they leave one entry out.
So the below code works today, but maybe not tomorrow, because the index number changed:
$xml = simplexml_load_file($xml_folder.$feed_file);
foreach($xml->product as $product) {
$country = $xml->product[$index]->properties->property[0]->value;
$region= $xml->product[$index]->properties->property[1]->value;
$servicetype = $xml->product[$index]->properties->property[3]->value;
$facilities = $xml->product[$index]->properties->property[13]->value;
$transporttype = $xml->product[$index]->variations->variation->property[4]->value;
// more code to put the data into html code
}
Is there any way to get the values by the name within the tag, not the index number? So something like this:
$servicetype = $xml->product[$index]->properties->property[name="serviceType"]->value;