Welcome! Log In Create A New Profile

Advanced

Exchange Rate RSS (XML) Parsing

Posted by DarrenF 
DarrenF
Exchange Rate RSS (XML) Parsing
November 20, 2008 01:11PM
All,

Just thought I'd post some anotated code here which takes an Exchange Rate RSS (XML) feed and parses the resulting XML to extract the currency types and exchange rates.

This code is based on a hard-coded GBP feed, but currencysource provide feeds for most of the major currencies.

My implementation holds the currency and exchange rate details on the same table as the country details, which means I have an additional loop in there for all the countries which may use the same currency, such as all the countries which use the Euro for example.

Obviously, this is a good example if you just want to know the processing required for obtaining and parsing XML in general.

Enjoy!

glocalbReqResult is boolean
glocalsTempstring is string
glocalsTempcurrency is string
glocalsTemprate is string
glocaliTempcount is int
glocalcTempChar is character

// Initiate the HTTP request
glocalbReqResult = HTTPRequest("[www.currencysource.com]winking smiley

// Are we ok to continue?
IF glocalbReqResult = True THEN
	
	// Move the resulting XML to the edit control for review on the window
	WEDIT_XML_View = HTTPGetResult()
	
	// Create a document based on this XML before we can begin
	XMLDocument("ExRates",WEDIT_XML_View)
	
ELSE
	// Can't find the XML!
	Info("The requested data feed cannot be located!")
	RETURN
END

// Look for the first 2 title tags and ignore them as they are just headers
XMLFind("ExRates","title",XMLTag)
XMLNext("ExRates")

// This is the first "real" title tag with a country code and rate
XMLNext("ExRates")

// Loop to process the rest of the tags
WHILE XMLFound("ExRates")

	// Move the current title tag data to a work variable
	glocalsTempstring = XMLData("ExRates")
	
	// Get currency code from the XML (it's in a fixed position)
	// This will be used later to read the country/currency table
	glocalsTempcurrency = Middle(glocalsTempstring,9,3)
	
	// Get exchange rate from the XML - it's enclosed in brackets, we need to parse char-by-char
	// Set the parse start point and clear the temp rate variable
	glocaliTempcount = 14
	glocalsTemprate = ""
	
	// Loop to extract the exchange rate - it's variable length
	// Parse the XML title data tag contents from tempcount (=14) 'til we reach the ")" bracket - this is the end of the rate data
	WHILE Middle(glocalsTempstring,glocaliTempcount,1) <> ")"
		
		// Save the character we are currently processing to avoid keep using the Middle function
		glocalcTempChar = Middle(glocalsTempstring,glocaliTempcount,1)
		
		// Ignore commas when parsing - they're not good when moving directly to a numeric field
		IF glocalcTempChar = "," THEN
			glocaliTempcount++	
		ELSE
			// Not a comma, so append the next digit to the exchange rate 
			glocalsTemprate = glocalsTemprate + glocalcTempChar
			glocaliTempcount++	
		END
	END
	
	// Now that we have the exchange rate, read the country table based on the currency code
       HReadSeek(Country,cCurrencyCode,glocalsTempcurrency)
        	
	// Set the country/currency exchange rate
	Country.nCurrencyRate = glocalsTemprate

	// Set the Last Updated Timestamp
	Country.dtCurrUpdated = DateSys() + TimeSys()
		
	// Modify the country/currency file with this new exchange rate
	HModify(Country)
		
	// Get the next country/currency as (in my implemtation) there are likely to be several countries using this currency (e.g. Euro)
	HReadNext(Country,cCurrencyCode)
		
	// Okay to continue?
	IF HFound(Country) THEN
			
		// Enter a loop to process countries using the same currency and drop out of loop when the currency changes
		WHILE Country.cCurrencyCode = glocalsTempcurrency
				
			// Set the exchange rate
			Country.nCurrencyRate = glocalsTemprate
			
			// Set the Last Updated Timestamp
			Country.dtCurrUpdated = DateSys() + TimeSys()
			
			// Modify the country file with this new exchange rate
			HModify(Country)
			
			// Get the next country/currency as there are likely to be several countries using the currency (e.g. Euro)
			HReadNext(Country,cCurrencyCode)
			
		END		
	END
			
	// Continue processing by finding the next title tag
	XMLNext("ExRates")
END

// The end...
XMLCancelSearch("ExRates")
XMLClose("ExRates")




Edited 1 time(s). Last edit at 12/12/2008 01:27PM by DarrenF.
DarrenF
Re: Exchange Rate RSS (XML) Parsing
December 11, 2008 09:31PM
All,

One addition to the above code... if you wish to re-run the code, don't forget to add the following to the end:

XMLClose("ExRates")
Jimbo
Re: Exchange Rate RSS (XML) Parsing
December 12, 2008 05:52AM
Hi Darren, you can EDIT your own postings ! It's one of the reasons why we chose MySnip.de ! Thank you for publishing your solution ! Kind regards, Guenter
DarrenF
Re: Exchange Rate RSS (XML) Parsing
December 12, 2008 01:28PM
Hi Guenter,

Thanks for the reminder :spos:

Ity's done... :xcool:#
Author:

Subject:


Spam prevention:
Please, enter the code that you see below in the input field. This is for blocking bots that try to post this form automatically. If the code is hard to read, then just try to guess it right. If you enter the wrong code, a new image is created and you get another chance to enter it right.
Message: