Link to home
Start Free TrialLog in
Avatar of electricd7
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("Microsoft.XMLHTTP").  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!
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

Open in new window

Avatar of LeroyJames
LeroyJames

City=xml.documentElement.selectSingleNode("//xml_ap_reply/forecast_information/city").getAttribute("data")
Avatar of electricd7

ASKER

Microsoft VBScript runtime error '800a01a8'

Object required: 'xml.documentElement.selectSingleNode(...)'

/test.asp, line 12
ASKER CERTIFIED SOLUTION
Avatar of LeroyJames
LeroyJames

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

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

Open in new window

dont load the responseXML load ResponseText
OK changed that to the following code, but still getting error:

Microsoft VBScript runtime error '800a01a8'

Object required: 'XMLDoc.documentElement.selectSingleNode(...)'

/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>"

Open in new window

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
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_reply version="1"><weather module_id="0" tab_id="0" mobile_row="0" mobile_z...
ok
I gave you a bad path     "//xml_api_reply/weather/forecast_information/city"
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>" 

Open in new window

check humidy you have humidty=repportTime=XMLDoc...... two equal signs
Doh!  Here's your points!
fyi  you can also do somthing like this

set xCurrNode=XMLDoc.documentElement.selectSingleNode("//xml_api_reply/weather/current_conditions")

if not(xCurrNode is nothing) then
      response.Write "<li>" & xCurrNode.selectSingleNode("temp_f").getattribute("data")
      response.Write "<li>" & xCurrNode.selectSingleNode("temp_c").getattribute("data")
      response.Write "<li>" & xCurrNode.selectSingleNode("humidity").getattribute("data")
      response.Write "<li>" & xCurrNode.selectSingleNode("wind_condition").getattribute("data")
      response.Write "<li>" & xCurrNode.selectSingleNode("icon").getattribute("data")
end if