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"?>
<!-- Edited with XML Spy v2006 (
http://www.altova.com) -->
<British_Birds>
<species>
<name>Golden oriole</name>
<latin>Oriolus oriolus</latin>
<status>Summer</status>
<breeding>9-42</breeding>
<passage>85</passage>
<Image ID="123" URI="golden_oriole.jpg"/>
<url address="www.rspb.org.uk/birds/guide/g/goldenoriole/index.asp" />
</species>
<species>
<name>Sparrowhawk</name>
<latin>Accipiter nisus</latin>
<status>Resident</status>
<breeding>34,500</breeding>
<passage>0</passage>
<Image ID="123" URI="sparrowhawk.jpg"/>
<url address="www.rspb.org.uk/birds/guide/s/sparrowhawk/index.asp" />
</species>
<species>
<name>Siskin</name>
<latin>Carduelis spinus</latin>
<status>Resident/Winter</status>
<breeding>310,000</breeding>
<passage>0</passage>
<Image ID="123" URI="siskin.jpg"/>
<url address="www.rspb.org.uk/birds/guide/s/siskin/index.asp" />
</species>
<species>
<name>Hoopoe</name>
<latin>Upupa epops</latin>
<status>Passage</status>
<breeding>Occasional</breeding>
<passage>100</passage>
<Image ID="123" URI="hoopoe.jpg"/>
<url address="www.rspb.org.uk/birds/guide/h/hoopoe/index.asp" />
</species>
</British_Birds>
<?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');
}
?>