|
DarrenF
Exchange Rate RSS (XML) Parsing November 20, 2008 01:11PM |
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]
// 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")
|
DarrenF
Re: Exchange Rate RSS (XML) Parsing December 11, 2008 09:31PM |
|
Jimbo
Re: Exchange Rate RSS (XML) Parsing December 12, 2008 05:52AM |
Moderator |
|
DarrenF
Re: Exchange Rate RSS (XML) Parsing December 12, 2008 01:28PM |