electricd7
asked on
Gettting xml values from google API in ASP classic
Hello,
I have the contents of the google weather XML file stored using server.createobject("Micro soft.XMLHT TP"). I just need to know how to get the values I want into variables. I found some code on the web and got most of it, but can't seem to get the last updated date/time. I just don't understand exactly what I am looking at. I will post the code below. I would really like to know how to get all the values into variables, but this is as far as I have gotten so far. The one I am primarily concerned with is reportTime. Thanks!
I have the contents of the google weather XML file stored using server.createobject("Micro
zip = "47804"
'Google API Weather Call
set xmlHTTP = server.createobject("Microsoft.XMLHTTP")
xmlHTTP.open "Get", "http://www.google.com/ig/api?weather="&Zip, false
xmlHTTP.send()
Set xml = xmlHTTP.ResponseXml
city = xml.documentElement.childNodes(0).firstChild.firstChild.attributes(0).nodeValue
currenttemp = xml.documentElement.childNodes(0).firstChild.nextSibling.childNodes(1).attributes(0).nodeValue
humidity = xml.documentElement.childNodes(0).firstChild.nextSibling.childNodes(3).attributes(0).nodeValue
conditionstext = xml.documentElement.childNodes(0).firstChild.nextSibling.childNodes(0).attributes(0).nodeValue
conditionsicon = xml.documentElement.childNodes(0).childNodes(2).childNodes(3).attributes(0).Value
winds = xml.documentElement.childNodes(0).firstChild.nextSibling.childNodes(5).attributes(0).nodeValue
reportTime = xml.documentElement.childNodes(1).firstChild.text
response.write City&"<br>"
response.write currenttemp&"<br>"
response.write conditionstext&"<br>"
response.write humidity&"<br>"
response.write winds&"<br>"
response.write "<img src='http://google.com/"&conditionsicon&"'><br>"
response.write reportTime
City=xml.documentElement.s electSingl eNode("//x ml_ap_repl y/forecast _informati on/city"). getAttribu te("data")
ASKER
Microsoft VBScript runtime error '800a01a8'
Object required: 'xml.documentElement.selec tSingleNod e(...)'
/test.asp, line 12
Object required: 'xml.documentElement.selec
/test.asp, line 12
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I am now running the following file, and getting the following error:
Microsoft VBScript runtime error '800a000d'
Type mismatch: 'XMLDoc.loadXML'
/test.asp, line 15
Microsoft VBScript runtime error '800a000d'
Type mismatch: 'XMLDoc.loadXML'
/test.asp, line 15
zip = "90210"
'Google API Weather Call
set xmlHTTP = server.createobject("Microsoft.XMLHTTP")
xmlHTTP.open "Get", "http://www.google.com/ig/api?weather="&Zip, false
xmlHTTP.send()
Set xml = xmlHTTP.ResponseXml
set XMLDoc=server.createobject("Microsoft.xmldom")
XMLDoc.validateonparse=false
XMLDoc.resolveexternals=false
XMLDoc.preservewhitespace=false
XMLDoc.async=false
bResult=XMLDoc.loadXML(xmlHTTP.ResponseXml)
City=XMLDoc.documentElement.selectSingleNode("//xml_ap_reply/forecast_information/city").getAttribute("data")
'city = xml.documentElement.childNodes(0).firstChild.firstChild.attributes(0).nodeValue
'City=xml.documentElement.selectSingleNode("//xml_ap_reply/weather/forecast_information/city").getAttribute("data")
'currenttemp = xml.documentElement.childNodes(0).firstChild.nextSibling.childNodes(1).attributes(0).nodeValue
'humidity = xml.documentElement.childNodes(0).firstChild.nextSibling.childNodes(3).attributes(0).nodeValue
'conditionstext = xml.documentElement.childNodes(0).firstChild.nextSibling.childNodes(0).attributes(0).nodeValue
'conditionsicon = xml.documentElement.childNodes(0).childNodes(2).childNodes(3).attributes(0).Value
'winds = xml.documentElement.childNodes(0).firstChild.nextSibling.childNodes(5).attributes(0).nodeValue
'reportTime = xml.documentElement.selectSingleNode("//xml_ap_reply/forecast_information/current_date_time").getAttribute("data")
response.write City&"<br>"
response.write currenttemp&"<br>"
response.write conditionstext&"<br>"
response.write humidity&"<br>"
response.write winds&"<br>"
response.write "<img src='http://google.com/"&conditionsicon&"' height=130 width=130><br>"
response.write reportTime
dont load the responseXML load ResponseText
ASKER
OK changed that to the following code, but still getting error:
Microsoft VBScript runtime error '800a01a8'
Object required: 'XMLDoc.documentElement.se lectSingle Node(...)'
/test.asp, line 17
Microsoft VBScript runtime error '800a01a8'
Object required: 'XMLDoc.documentElement.se
/test.asp, line 17
zip = "90210"
'Google API Weather Call
set xmlHTTP = server.createobject("Microsoft.XMLHTTP")
xmlHTTP.open "Get", "http://www.google.com/ig/api?weather="&Zip, false
xmlHTTP.send()
Set xml = xmlHTTP.ResponseXml
set XMLDoc=server.createobject("Microsoft.xmldom")
XMLDoc.validateonparse=false
XMLDoc.resolveexternals=false
XMLDoc.preservewhitespace=false
XMLDoc.async=false
bResult=XMLDoc.loadXML(XMLHTTP.Responsetext)
City=XMLDoc.documentElement.selectSingleNode("//xml_ap_reply/forecast_information/city").getAttribute("data")
response.write City&"<br>"
after the bResult=XMLDOc.loadXML...
add
response.write "<li>" & bResult
response.write "<textarea rows=20 cols=80>" & XMLHTTP.responseText & "</textarea>'
response.write "<li>" & XMLDoc.parseError.reason
add
response.write "<li>" & bResult
response.write "<textarea rows=20 cols=80>" & XMLHTTP.responseText & "</textarea>'
response.write "<li>" & XMLDoc.parseError.reason
ASKER
Invalid at the top level of the document. Error processing resource 'http://myserver.com/test.asp'. Line 1, Position 1
True<?xml version="1.0"?><xml_api_re ply version="1"><weather module_id="0" tab_id="0" mobile_row="0" mobile_z...
True<?xml version="1.0"?><xml_api_re
ok
I gave you a bad path "//xml_api_reply/weather/f orecast_in formation/ city"
I gave you a bad path "//xml_api_reply/weather/f
ASKER
Ok that works for city, but now I can't the ones in current_conditions. Here is what I have. The page shows "False" for all those nodes now. Here is the code:
zip = "90210"
'Google API Weather Call
set xmlHTTP = server.createobject("Microsoft.XMLHTTP")
xmlHTTP.open "Get", "http://www.google.com/ig/api?weather="&Zip, false
xmlHTTP.send()
Set xml = xmlHTTP.ResponseXml
set XMLDoc=server.createobject("Microsoft.xmldom")
XMLDoc.validateonparse=false
XMLDoc.resolveexternals=false
XMLDoc.preservewhitespace=false
XMLDoc.async=false
bResult=XMLDoc.loadXML(XMLHTTP.Responsetext)
City=XMLDoc.documentElement.selectSingleNode("//xml_api_reply/weather/forecast_information/city").getAttribute("data")
ReportTime=XMLDoc.documentElement.selectSingleNode("//xml_api_reply/weather/forecast_information/current_date_time").getAttribute("data")
Condition=ReportTime=XMLDoc.documentElement.selectSingleNode("//xml_api_reply/weather/current_conditions/condition").getAttribute("data")
temp=ReportTime=XMLDoc.documentElement.selectSingleNode("//xml_api_reply/weather/current_conditions/temp_f").getAttribute("data")
humidity=ReportTime=XMLDoc.documentElement.selectSingleNode("//xml_api_reply/weather/current_conditions/humidity").getAttribute("data")
winds=ReportTime=XMLDoc.documentElement.selectSingleNode("//xml_api_reply/weather/current_conditions/wind_condition").getAttribute("data")
icon=ReportTime=XMLDoc.documentElement.selectSingleNode("//xml_api_reply/weather/current_conditions/icon").getAttribute("data")
response.write City&"<br>"
response.write ReportTime&"<br>"
response.write condition&"<br>"
response.write temp&"<br>"
response.write humidity&"<br>"
response.write winds&"<br>"
response.write "<img src='http://google.com/"&icon&"'><br>"
check humidy you have humidty=repportTime=XMLDoc ...... two equal signs
ASKER
Doh! Here's your points!
fyi you can also do somthing like this
set xCurrNode=XMLDoc.documentE lement.sel ectSingleN ode("//xml _api_reply /weather/c urrent_con ditions")
if not(xCurrNode is nothing) then
response.Write "<li>" & xCurrNode.selectSingleNode ("temp_f") .getattrib ute("data" )
response.Write "<li>" & xCurrNode.selectSingleNode ("temp_c") .getattrib ute("data" )
response.Write "<li>" & xCurrNode.selectSingleNode ("humidity ").getattr ibute("dat a")
response.Write "<li>" & xCurrNode.selectSingleNode ("wind_con dition").g etattribut e("data")
response.Write "<li>" & xCurrNode.selectSingleNode ("icon").g etattribut e("data")
end if
set xCurrNode=XMLDoc.documentE
if not(xCurrNode is nothing) then
response.Write "<li>" & xCurrNode.selectSingleNode
response.Write "<li>" & xCurrNode.selectSingleNode
response.Write "<li>" & xCurrNode.selectSingleNode
response.Write "<li>" & xCurrNode.selectSingleNode
response.Write "<li>" & xCurrNode.selectSingleNode
end if