Steve’s Ubuntu Weblog

Mainly (but not only) about Ubuntu

I have made a currency convertor which uses the daily updated European Central Banks exchange rates. You can convert between 35 different currencies.

The converter can be found here.

The previous post shows how I download and upload the ECB XML file.

9 November, 2008 Posted by steveyoung | Uncategorized | | No Comments Yet

Using the ECB Currency XML with Javascript

The European Central Bank make available daily an XML file with the exchange rate of the Euro to 34 currencies. This can be found at http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml

To use this in javascript I faced two problems:

1) How to get around the inter-domain problem. The easiest way to do this is with PHP/Curl but my ISP does not support php! I solved this by downloading the file and uploading it to my webserver. First I GET the file from the CEB website.

<?php
function get_file1($file, $local_path, $newfilename) {
$out = fopen($newfilename, 'wb');
if ($out == FALSE){
print "File not opened<br>";
exit;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_FILE, $out);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, $file);
curl_exec($ch);
curl_close($ch);
}

and the I PUT the file onto my server.

$url = "ftp://my.server.at/ECBRates.xml";
$localfile = "/var/www/ECBRates";
$fp = fopen ($localfile, "r");
$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_USERPWD, 'user:password');
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_PUT, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localfile));
$http_result = curl_exec($ch);
$error = curl_error($ch);
$http_code = curl_getinfo($ch ,CURLINFO_HTTP_CODE);
curl_close($ch);
fclose($fp);
print $http_code;
print "<br /><br />$http_result";
if ($error) {
print "<br /><br />$error";
}
?>

Copy these into a single document and name it xmlgetandput.php and run it using curl:

curl localhost/xmlgetandput.php

If for whatever reason you are unable to do this yourself I can upload the file (or any other file) to your webserver daily for fee of €5 per month. Just make a comment with the title “upload” and I will contact you via email with details.

2) Loading and parsing an XML file is different for DOM compliant browsers and for Internet Explorer. I found this simple for the DOM complient browsers but it took me over a week of googling to get the IE working. The problem was further compounded by the fact that the ECB Currency XML has a strange hierarchy. But I got it working with the following javascript code.

var doc
function load() {
if (document.implementation &&document.implementation.createDocument){
doc = document.implementation.createDocument("", "", null);
doc.load('CEBrates.xml');
doc.onload = createTableMoz;
}
else if (window.ActiveXObject){
var doc1 = new ActiveXObject('Msxml2.DOMDocument.3.0');
function loadXML(xmlFile){
doc1.async=false;
doc1.load(xmlFile);
doc1.setProperty("SelectionLanguage", "XPath");
var nsMappings = "xmlns:gesmes='http://www.gesmes.org/xml/2002-08-01' " + "xmlns:default='http://www.ecb.int/vocabulary/2002-08-01/eurofxref'";
doc1.setProperty("SelectionNamespaces", nsMappings);
var xpath = "/gesmes:Envelope/default:Cube/default:Cube";
var cubeMain = doc1.selectNodes(xpath)
var dateRate = cubeMain[0].getAttribute('time');
var dateRateSplit = dateRate.split('-');
xpath = "/gesmes:Envelope/default:Cube/default:Cube/default:Cube";
var cubes = doc1.selectNodes(xpath);
for (var i = 2; i < cubes.length; i++){
var currency = cubes[i].getAttribute('currency');
var rate = cubes[i].getAttribute('rate');
rateObject[currency] = rate;
}
document.getElementById('boldStuff').innerHTML = dateRateSplit[2] + "." + dateRateSplit[1] + "." + dateRateSplit[0];
document.getElementById("currency").value = "GBP";
getRates();
}
loadXML('CEBrates.xml');
}
else {
alert('ActiveX must be enabled for this feature');
return;
}
}
function createTableMoz() {
var cubes = doc.getElementsByTagNameNS('http://www.ecb.int/vocabulary/2002-08-01/eurofxref','Cube');
var dateRate = cubes[1].getAttribute('time');
var dateRateSplit = dateRate.split('-');
for (var i = 2; i < cubes.length; i++){
var currency = cubes[i].getAttribute('currency');
var rate = cubes[i].getAttribute('rate');
rateObject[currency] = rate;
}
document.getElementById('boldStuff').innerHTML = dateRateSplit[2] + "." + dateRateSplit[1] + "." + dateRateSplit[0];
document.getElementById("currency").value = "GBP";
getRates();
};
var rateObject = {};
function getRates() {
var curr= document.getElementById("currency").value;
var currRate = rateObject[curr];
var currStatement= "1 EUR = " + currRate + " " + curr ;
document.getElementById("rate").value=currStatement;
var currRev=1/currRate;
currRevFix=currRev.toFixed(5);
var currRevStatement= "1 " + curr +"= " + currRevFix + " EUR";
document.getElementById("rateRev").value=currRevStatement;
};

Here is a link to the finished product http://members.chello.at/stephen.joung/indexCC.html . You might find the currency converter useful.

8 November, 2008 Posted by steveyoung | Uncategorized | , , , , , , , , , , | 3 Comments