Forum Moderators: open
function to get currency exchange rate from RSS feed
function getCurrency( sCurrency )
sBasePrice = "USD"
//sBasePrice = "CAD"
Response.Charset = "UTF-8"
Dim objXML
Dim objItemList
Dim objItem
Dim nCurrency
Set objXML = Server.CreateObject( "MSXML2.FreeThreadedDOMDocument" )
objXML.async = False
objXML.setProperty "ServerHTTPRequest", True
objXML.Load( "http://currencysource.com/RSS/" & sBasePrice & ".xml" )
' set currency exchange rate to 1 CAD = 1 USD if error with RSS feed
nCurrency = 1
' no errors
If objXML.parseError.errorCode = 0 Then
Set objItemList = objXML.getElementsByTagName( "item" )
Set objXML = Nothing
'RSS Feed childNodes: 0=title, 1=link, 2=description, 3=pubdate
For Each objItem In objItemList
' check if current item is USD
' 1 CAD = USD (0.861966)
if Mid( objItem.childNodes( 0 ).text, 1, 11 ) = "1 " & sBasePrice & " = " & sCurrency then
nCurrency = Left( Mid( objItem.childNodes( 0 ).text, 14 ), Len( Mid( objItem.childNodes( 0 ).text, 14 ) ) - 1 )
exit for
end if
Next
Set objItemList = Nothing
End If
getCurrency = nCurrency
end function
<title>1 USD = CAD (1.124512)</title> * * *
so. what is returned from getCurrency("CAD") ?
I don't see anything glaringly wrong with the function. Nothing obvious anyways.
debugging tip: inject little debugging one-liner commands into your script and echo out variables to see what their values are.
for instance, echo out "objItem.childNodes( 0 ).text" and see if it is what you expect.
Then echo out "Mid( objItem.childNodes( 0 ).text, 1, 11 )". Take the code in little chunks and verify that it's doing everything right from top to bottom.
It may be something as simple as a bad boundary limit in the Mid() or Left() string functions.
Also, you may consider using a regular expression to extract the data instead of awkwardly nested substring functions.
sBasePrice = "USD"
objXML.Load( "http://currencysource.com/RSS/" & sBasePrice & ".xml" )
thus the URL being requested is actually
[currencysource.com...]
really I think the problem is something subtle in the way the data is being parsed. Or maybe it's a syntax error somewhere? I don't have the right machinery running to try your code sample out myself, and I'm very rusty on my VBScript syntax.
Debugging is a tough job, and it's a skill that takes practice. Start by "tracing [en.wikipedia.org]" values to see what they are during the execution of the code. Fixing this function should take a few minutes, not several days.
Tracing is probably the most important debugging technique you must learn, and it's much simpler than, say, bug discovery by systematic amputation, or dissecting the call stack.
The key here is to learn some debugging techniques so you can find out what the problem is. I'm guessing that once you figure out the problem, the solution will be immediately obvious.