![]() |
|
|||
|
The currency and the value are read from an xml file. I would then like to add them to an object. I have tried various permutations of the following (ie using square brackets instead of dot etc) without success. <SCRIPT TYPE="text/javascript" LANGUAGE="JavaScript"> var doc = document.implementation.createDocument('', 'dummy', null); doc.load('CEBrates.xml'); var rateObject = {}; doc.onload = function () { var cubes = doc.getElementsByTagNameNS('http://www.ecb.int/ vocabulary/2002-08-01/eurofxref','Cube'); var date = cubes[1].getAttribute('time'); document.write("Data for " + date + "<br /><br />") for (var i = 2; i < cubes.length; i++){ var currency = cubes[i].getAttribute('currency'); var rate = cubes[i].getAttribute('rate'); rateObject.currency = rate; } document.write(rateObject.USD); document.close() }; </script> I would be grateful for any help. <steve. |
|
|||
|
On 2008-09-26 22:23, Steve wrote:
> The currency and the value are read from an xml file. I would then > like to add them to an object. I have tried various permutations of > the following (ie using square brackets instead of dot etc) without > success. .... > for (var i = 2; i < cubes.length; i++){ > var currency = cubes[i].getAttribute('currency'); > var rate = cubes[i].getAttribute('rate'); > rateObject.currency = rate; You are setting the "currency" property of the rateObject object, not the "USD" or "GBP" properties. What you probably want as a result is something like this (I didn't look too closely): { 'USD': '123.456', 'GBP': '234.345', 'EUR': '345.456', (etc) } Try using this instead: rateObject[currency] = rate; That's assuming that all the expected elements and attributes actually exist and are not empty. Also, the values will be Strings, not Numbers, but I guess you know that. Just in case :-) - Conrad |
|
|||
|
On Sep 27, 4:23*am, Steve <stephen.jo...@googlemail.com> wrote:
> the following (ie using square brackets instead of dot etc) without > success. > <snip> > * * * * var currency = cubes[i].getAttribute('currency'); > * * * * var rate = cubes[i].getAttribute('rate'); > * * * * rateObject.currency = rate; /* what you probably want is: */ rateObject[currency] = rate; /* so that later you can use something like: */ var usdRate = rateObject.USD; var myrRate = rateObject.MYR; |
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|