Forum Moderators: coopster

Message Too Old, No Replies

Parsing XML file on remote server

European Central Bank Exchange rates

         

Notawiz

2:10 pm on Aug 30, 2004 (gmt 0)

10+ Year Member



The ECB very conveniently (and for free) gives daily the exchange rates of the most common currencies, in several file formats, among which an XML file for parsing. (shortened copy below)

<?xml version="1.0" encoding="UTF-8"?>
<gesmes:Envelope xmlns:gesmes="http://www.gesmes.org/xml/2002-08-01" xmlns="http://www.ecb.int/vocabulary/2002-08-01/eurofxref">
<gesmes:subject>Reference rates</gesmes:subject>
<gesmes:Sender>
<gesmes:name>European Central Bank</gesmes:name>
</gesmes:Sender>
<Cube>
<Cube time='2004-03-11'>
<Cube currency='USD' rate='1.2256'/>
<Cube currency='JPY' rate='135.58'/>
<Cube currency='DKK' rate='7.4518'/>
....
<Cube currency='ZAR' rate='8.2441'/>
</Cube>
</Cube>
</gesmes:Envelope>

I've done extensive searching in all known forums, but didn't find how to write the php code to do the parsing of this kind of file.

What I want to do is to check first the date, to avoid having twice the same entry, and if not yet in DB, insert the new values in the mysql table.

Now all the last part I can write, but not the parsing of the "cube" thing...

Has anyone already tried this one?
Since this is a free resource, perhaps you'll allow me to post also the URI for the XML file:
[ecb.int...]

Thanx for any suggestion.
Notawiz

Timotheos

3:32 pm on Aug 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Maybe this thread will help...
[webmasterworld.com...]

Notawiz

5:14 pm on Aug 30, 2004 (gmt 0)

10+ Year Member



Hello Timotheos,

For "classic" xml, things work not too bad with the tools we have in PHP, when we have <openingTag>some string</closingTag>.

But when you look at the file of the EBC, one founds all kind of stuff inside similar "Cube" tags, like the time for the first one, and then currency and rate.

I understand that the author of that file wants to have something like an array called "time", with keys called "currency" and values called "rate".

How to reconstruct a classical php array from these "cube" data, that's my problem.

Thanx.
Notawiz

dmorison

5:56 pm on Aug 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



See MSG#8 in this thread...

[webmasterworld.com...]

...for PHP code to extract an exchange rate from the ECB feed.

Timotheos

6:10 pm on Aug 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ah I get it. That's sweet dmorrison. Looks like inbuilt support in PHP to snag attributes is still experimental (DOM XML and SimpleXML).

Notawiz

6:19 pm on Aug 30, 2004 (gmt 0)

10+ Year Member



Waw dmorison, I'm impressed!

So if I want to store all the rates of the ECB file, I just have to walk through the file by changing the:
$EUR_GBP_RATE = $xp->getAttributes("//Cube[@currency='GBP']","rate");
into say:
$EUR_USD_RATE = $xp->getAttributes("//Cube[@currency='USD']","rate");
and at the end store the whole bunch in my DB.

Great help!
Thanx.
Notawiz