PHP XML Counting Elements

They have: 18 posts

Joined: Mar 2006

Hi

I have some PHP code that reads the content of elements from an XML file. I have used for ( $counter = 0; $counter <= $count; ++$counter) to move from one parent element to the next and set $count = 3 because i know that the XML file has 4 parent elements. My problem is that the XML files I have contain different amounts of parent elements, i would like to use code to count them so i can set $count = $no_of_elements. Also it'd be cool to use code to find out what the element names are. I am using PHP Version 5.0.3 so simplexml & DOM/XML and Simple/XML are enabled, although i haven't yet found the appropriate functions for the job. Any advice would be great. PHP & XML code thus far is pasted below.

Cheers

Don

<?xml version="1.0" encoding="ISO-8859-1"?>

Golden oriole
Oriolus oriolus
Summer
9-42
85

Sparrowhawk
Accipiter nisus
Resident
34,500
0

Siskin
Carduelis spinus
Resident/Winter
310,000
0

Hoopoe
Upupa epops
Passage
Occasional
100

<?php
$database
="birds";
$user="root";
mysql_connect(localhost,$user);
@
mysql_select_db($database) or die( "Unable to select database");


if (
file_exists('birds5.xml')) {
     
$xml = simplexml_load_file('birds5.xml');

$count = 3;

for (
$counter = 0; $counter <= $count; ++$counter) {

$image = $xml->species[$counter]->image;
$name = $xml->species[$counter]->name;
$latin = $xml->species[$counter]->latin;
$status = $xml->species[$counter]->status;
$breeding = $xml->species[$counter]->breeding;
$passage= $xml->species[$counter]->passage;
$url = $xml->species[$counter]->url;


mysql_query("INSERT INTO allbirds
(image,name,latin,status,breeding,passage,url) VALUES('
$image','$name', '$latin','$status', '$breeding','$passage', '$url') ")
or die(
mysql_error());

}


      }
else
{
exit(
'failed');
}
?>