I'm using simpleXML to parse a xml file and want to iterate through the elements using a foreach loop and store it to a mysql database with help of a Yii model.
I have a sample xml file containing 1083 entries each having three attributes. When using PHP version 5.3.1 on Windows it creates 1208 entries in the database table instead of 1083. I tried to find the problem in my code but after hours of testing I decided to switch to PHP version 5.4.7 on Windows. In this version it creates 1083 entries which is correct. But I want to find out what the problem exactly is because I need to find a solution because on my productive environment on a linux server I have PHP version 5.4.15-1 which also creates a wrong number of entries.
That's some example xml:
<listentries>
<listentry>
<name>downs</name>
<value>-1.5</value>
<type>STATIC</type>
</listentry>
<listentry>
<name>upcoming</name>
<value>1.0</value>
<type>STATIC</type>
</listentry>
...
</listentries>
So my problem is that the foreach loop is iterating too often.
$list = simplexml_load_file($filename);
foreach($list->listentries->listentry as $entry){
$model = new Entry;
$model->name = $entry->name;
$model->value = $entry->value;
$model->type = $entry->type;
$model->save();
}
When echoing the number of list elements I get 1083. When using a counter for counting the iterations I get 1208. So something very weird is going on. The 1208 inserted entries in the database looks the following: each entry is inserted at least once but some are inserted twice.
Does anybody have an idea where the missbehavior is coming from exactly?
I found out that the correct number of iterations is done when the xml file contains less than 970 entries.
My configurations and number of inserted entries after importing xml containing 1083 entries:
- Windows, PHP 5.3.1, libxml 2.7.6: 1208 entries
- Windows, PHP 5.4.7, libxml 2.7.8: 1083 entries
- Linux, PHP 5.4.15-1, libxml 2.7.6: 1208 entries
So it looks like updating libxml could help!?