Retrieving live exchange rates from ECB XML feed for iOS

Exchange rates from the ECB using XML for Objective-cWhile building the Amazon Associates application I needed to access current exchange rates for a number of different currencies.  I looked for various sources for the data found the ECB and Bank of Canada both offer XML exchange rate feeds.

  • http://www.bankofcanada.ca/rates/exchange/rates-in-rss/
  • http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml

The ECB one was chosen because it’s much reduced data feed was all I that needed.  The feed looks like this:


	Reference rates
	
		European Central Bank
	
	
		
			
			

			
			
			
			
			
			
			
			
			

			
			
			
			
			
			
			
			
			

			
			
			
			
			
			
			
			
			

			
			
			
			
		
	

To retrieve the feed within the application I used the TBXML library as it provided simple access to XML element attributes whereas with the more commonly used TouchCode/TouchXML library this appeared to be more difficult. So after downloading the TBXML library and importing it into the project and enabling libXML2 I used two other functions as follows to retrieve the data:

- (void)loadExchangeRates {
	
	// initialize rate array
	exchangeRates = [[NSMutableDictionary alloc] init];
	
	// Load and parse the rates.xml file
	TBXML * tbxml = [[TBXML tbxmlWithURL:[NSURL URLWithString:@"http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml"]] retain];
	
	// If TBXML found a root node, process element and iterate all children
	if (tbxml.rootXMLElement)
		[self traverseElement:tbxml.rootXMLElement];
	
	// add EUR to rate table
	[exchangeRates setObject:@"1.0" forKey:@"EUR"];
	
	// release resources
	[tbxml release];
}

- (void) traverseElement:(TBXMLElement *)element {
	
	do {
		// Display the name of the element
		//NSLog(@"%@",[TBXML elementName:element]);
		
		// Obtain first attribute from element
		TBXMLAttribute * attribute = element->firstAttribute;
		
		// if attribute is valid
		NSString *currencyName;
		while (attribute) {
			/* Display name and value of attribute to the log window
			NSLog(@"%@->%@ = %@",
				  [TBXML elementName:element],
				  [TBXML attributeName:attribute],
				  [TBXML attributeValue:attribute]);
			*/
			// store currency
			if ([[TBXML attributeName:attribute] isEqualToString: @"currency"]) {
				currencyName = [TBXML attributeValue:attribute];
			}else if ([[TBXML attributeName:attribute] isEqualToString: @"rate"]) {
				// store currency and rate in dictionary
				[exchangeRates setObject:[TBXML attributeValue:attribute] forKey:currencyName];
			}
			// Obtain the next attribute
			attribute = attribute->next;
		}
		
		// if the element has child elements, process them
		if (element->firstChild) 
            [self traverseElement:element->firstChild];
		
		// Obtain next sibling element
	} while ((element = element->nextSibling));  
}

The traverseElement method references a variable exchangeRates. This is declared in your class’s header file (.h) as NSMutableDictionary *exchangeRates; and synthesized in your class’s implementation file.

In my case I called the loadExchangeRates() method in my class’s viewDidLoad() method. If after making the call to load the rates, you log the contents of the exchangeRates you should see each currency’s name use as the key to the current exchange rate for that currency as follows:

2011-05-17 15:36:17.901 Associates[55765:40b] Exchange rates: {
    AUD = "1.3393";
    BGN = "1.9558";
    BRL = "2.3124";
    CAD = "1.3818";
    CHF = "1.2553";
    CNY = "9.2195";
    CZK = "24.454";
    DKK = "7.4566";
    EUR = "1.0";
    GBP = "0.87290";
    HKD = "11.0178";
    HRK = "7.4150";
    HUF = "267.69";
    IDR = "12153.35";
    ILS = "5.0100";
    INR = "63.9500";
    JPY = "115.69";
    KRW = "1550.67";
    LTL = "3.4528";
    LVL = "0.7092";
    MXN = "16.6550";
    MYR = "4.3200";
    NOK = "7.9385";
    NZD = "1.8157";
    PHP = "61.422";
    PLN = "3.9288";
    RON = "4.1115";
    RUB = "39.9175";
    SEK = "9.0015";
    SGD = "1.7695";
    THB = "42.938";
    TRY = "2.2484";
    USD = "1.4171";
    ZAR = "9.9056";
}

So, if you now wanted to use the EUR/USD exchange rate in your application you can now call:

NSDecimalNumber *rate = [NSDecimalNumber decimalNumberWithString:[self.exchangeRates objectForKey:@"USD"]];

and access it using the rate variable.

Tags: , , ,

No comments yet.

Leave a Reply