I've been through the 'Lord of the Rings' themed example of how to get data from XML files, but I can't figure out how to get individual peices of data from an XML file.
For example, I have a US Dollars to Euros, Pounds, Canadian Dollars and Australian Dollars currency converter on several websites, and instead of having to manually edit the exchange rates in each file, I would prefer to edit just one XML file and have the exchange rates called from that.
The XML file I have created looks like:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE exchange_rates [
<!ELEMENT currency (name, rate)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT rate (#PCDATA)>
]>
<exchange_rates>
<currency nation="European Union">
<name>Euro</name>
<rate>0.890234</rate>
</currency>
<currency nation="United Kingdom">
<name>Pound</name>
<rate>0.625547</rate>
</currency>
<currency nation="Canada">
<name>Canadian Dollar</name>
<rate>1.3927</rate>
</currency>
<currency nation="Australia">
<name>Australian Dollar</name>
<rate>1.53421</rate>
</currency>
</exchange_rates>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
and the convertor file currently looks like this:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<p align="center">
<b>[u]
Currency Converter
</u></b>
</p>
<p align="left">
<?php
if( ! ($fp = fopen("http://www.starsol.co.uk/scripts/files/currency_ex.xml" , " r" )) )
die("Unable to open XML file.");
$currency_counter = 0;
$currency_data = array();
$xml_current_tag_state ='';
function startElementHandler( $parser, $element_name , $element_attribs )
{
global $currency_counter;
global $currency_data;
global $xml_current_tag_state;
if( $element_name == "CURRENCY" )
{
$currency_data[$currency_counter]["nation"] = $element_attribs["NATION"];
}
else
{
$xml_current_tag_state = $element_name;
}
}
function endElementHandler( $parser, $element_name )
{
global $currency_counter;
global $currency_data;
global $xml_current_tag_state;
$xml_current_tag_state = '';
if( $element_name = "CURRENCY" )
{
$currency_counter++;
}
}
function characterDataHandler( $parser , $data )
{
global $currency_counter;
global $currency_data;
global $xml_current_tag_state;
if( $xml_current_tag_state == '' )
return;
if( $xml_current_tag_state == "NAME" ) {
$currency_data[$currency_counter]["name"] = $data;
}
if( $xml_current_tag_state == "RATE" ) {
$currency_data[$currency_counter]["rate"] = $data;
}
}
if( !($xml_parser = xml_parser_create()) )
die("Couldn't create XML parser.");
xml_set_element_handler($xml_parser, "startElementHandler", "endElementHandler");
xml_set_character_data_handler($xml_parser, "characterDataHandler");
while( $data = fread($fp, 4096) )
{
if( !xml_parse($xml_parser, $data, feof($fp)) )
{
break;
}
}
xml_parser_free($xml_parser);
for( $i=0 ; $i < $currency_counter ; ++$i )
{
echo $currency_data[$i]["name"];
echo $currency_data[$i]["rate"];
echo $currency_data[$i]["nation"];
if ($currency == "Euro"){
$convamount = $amount * $eurorate;
$convamount = round($convamount, 2);
echo"<b>$amount</b> US Dollars is approximately equal to <b>$convamount</b> Euros.";
}
if ($currency == "Pound"){
$convamount = $amount * $poundrate;
$convamount = round($convamount, 2);
echo"<b>$amount</b> US Dollars is approximately equal to <b>$convamount</b> Pounds.";
}
if ($currency == "Canadian Dollar"){
$convamount = $amount * $canadarate;
$convamount = round($convamount, 2);
echo"<b>$amount</b> US Dollars is approximately equal to <b>$convamount</b> Canadian Dollars.";
}
if ($currency == "Australian Dollar"){
$convamount = $amount * $ausrate;
$convamount = round($convamount, 2);
echo"<b>$amount</b> US Dollars is approximately equal to <b>$convamount</b> Australian Dollars.";
}
?>
<br><br>
<i>Please note, that the exchange rates are updated manually, and may not be precisely accurate.</i>
</p>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
(The echos for $currency_data[$i]["name"]; and so on are just so I can check the calls are working.)
What I need to do is get the values for $eurorate, $poundrate, $canadarate and $ausrate from the XML file. I'm sure it's relatively simple, but I can't find anywhere that tells me where to do it.
Can anyone rescue me?
Thanks and best wishes,
Rupe

.
Rupe Parnell
www.starsol.co.uk