Forum Moderators: coopster

Message Too Old, No Replies

Parsing Duplicate XML Child Nodes

         

clickshops

4:55 pm on Aug 26, 2009 (gmt 0)

10+ Year Member



I am attempting to parse an XML feed into a mysql database. Everything is working well until I encountered the part of the feed below. As you can see, the feed has 5 fields called <facility> and my parser is only reading the first one "Kids Club".

Is it possible to extract the other 4 and get them into Mysql?

---------------------------------------------------------

<facilities>
<facility>Kids Club</facility>
<facility>Wavelength</facility>
<facility>Sports &amp; Activities</facility>
<facility>Children's programme</facility>
<facility>Free kids</facility>
</facilities>

coopster

3:15 pm on Aug 28, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You'll need to use a looping construct. There are some good examples [php.net] on the PHP SimpleXML manual pages.

brodyh

6:40 am on Aug 31, 2009 (gmt 0)

10+ Year Member



Assuming that <facilities> is the top-level node, use:

$MyVar = "";
foreach($XML->facility as $facility) {
$MyVar .= $facility . ", ";
}

This would output the names of the facilities in a comma separated list into the variable $MyVar.

clickshops

5:33 pm on Sep 1, 2009 (gmt 0)

10+ Year Member



Thanks to both of you. Both replies have helped me, but I still seem to have a problem. Initally I was only getting the first entry "Kids Club" inserted into the data base, now I have used the tips above, I'm getting the last entry "Free kids" inserted into the database. I suspect that my parser is overwriting the previous entries during the foreach loop and ending at the last entry, leaving me with "Free kids" in the Mysql table.

I was hoping to achieve the following:-
In 5 seperate Mysql columns called Facility1, Facility2, Facility3, Facility4, Facility5 I would have captured the following 5 entries and inserted them in the individual columns. Into Facility1 I would have "Kids Club", Facility2 = "Wavelength", Facility3 = "Sports &amp; Activities", Facility4 = "Children's programme" and finally Facility5 = "Free kids".

Any more help really appreciated.

Pete