You are here:  » XML file using cubes


XML file using cubes

Submitted by lumby on Fri, 2007-06-01 15:02 in

I am a newbie at both ZML and php

My ZML feed is http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml

The feed uses 'cubes' which has fooled me

I would like the result to be similar to
$currency1, $ rate1
$currency2, $ rate2
etc

I intend to use php to multiply a price in Euros by the relevant currency rate to give a price in local currency

Submitted by support on Fri, 2007-06-01 15:17

Hi there,

I've worked with the ECB exchange rate feed before - it works nicely with Magic Parser, although i'm not really sure where the "cubes" come from - it's not something i've ever come across anywhere else!

Anyway, to process the feed using Magic Parser, you need to use the following format string:

xml|GESMES:ENVELOPE/CUBE/CUBE/CUBE/

Using this, your record handler function will be called once for each currency in the feed. What I recommend doing is rather than trying to create separate variables for each currency ($currency1, $rate1 etc.), that you load the exchange rates into an array. Then, after parsing you can reference the array with the currency you want to convert by - which will give you the exchange rate.

Here's the basic code to parse the XML and load the array:

<?php
  
require("MagicParser.php");
  
$rates = array();
  function 
myRecordHandler($record)
  {
    global 
$rates;
    
$currency $record["CUBE-CURRENCY"];
    
$rate $record["CUBE-RATE"];
    
$rates[$currency] = $rate;
  }
  
$url "http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml";
  
MagicParser_parse($url,"myRecordHandler""xml|GESMES:ENVELOPE/CUBE/CUBE/CUBE/");
  
print_r($rates);
?>

Here's the output from that script running on this server:

http://www.magicparser.com/examples/cubes.php

In that script, the print_r() function just displays the contents of the $rates array so that you can see if your code has worked. Having obtained your array, if you're new to PHP and not sure how to go about using an associative array here's an example to convert, for example, 100 Euro into US Dollars:

<?php
  
require("MagicParser.php");
  
$rates = array();
  function 
myRecordHandler($record)
  {
    global 
$rates;
    
$currency $record["CUBE-CURRENCY"];
    
$rate $record["CUBE-RATE"];
    
$rates[$currency] = $rate;
  }
  
$url "http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml";
  
MagicParser_parse($url,"myRecordHandler""xml|GESMES:ENVELOPE/CUBE/CUBE/CUBE/");
  
$amount_eur 100.00// amount in EUR
  
$amount_usd $amount_eur $rates["USD"]; // calculate amount in USD
  
print "EUR ".$amount_eur." = USD ".$amount_usd;
?>

Again, here's the output from that script running on this server:

http://www.magicparser.com/examples/cubes2.php

Hope this helps get you started!
Cheers,
David.