如何在线更改世界天气中的图标

时间:2012-12-17 15:59:41

标签: xml asp-classic weather-api

我使用Classic asp和XML在线使用世界天气实现了一个天气小部件,一切似乎都运行良好,但我想知道是否有人知道如何更改图标?

由于

    <%
Function getweather(city)
    Dim tempC, wDesc, wIcon, currentWeather
    Dim wDate, wHigh, wLow, extWeather

    url = "http://free.worldweatheronline.com/feed/weather.ashx?q=" & city & ",Canada&format=xml&num_of_days=4&key=********" 
  set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP") 
  xmlhttp.open "GET", url, false 
  xmlhttp.send "" 

    set objXML = Server.CreateObject("MSXML2.DOMDocument")
    objXML.async = false    
    objXML.loadxml xmlhttp.responseText

    set x = objXML.getElementsByTagName("current_condition")
    if x.length = 0 then
        retStr = "Error"
    else        
        for i = 0 to x.item(0).childNodes.length - 1
            if x.item(0).childNodes(i).nodeName = "temp_C" then
                tempC = x.item(0).childNodes(i).text & "&deg;C"
            end if
            if x.item(0).childNodes(i).nodeName = "weatherDesc" then
                wDesc = x.item(0).childNodes(i).text
            end if
            if x.item(0).childNodes(i).nodeName = "weatherIconUrl" then
                wIcon = "<img class='wIcon' src=" & x.item(0).childNodes(i).text & " height=45 width=45>"
            end if
        next            
        currentWeather = "<div id='curWeather'><span class='city'>" & city & "</span>" & wIcon & "<div class='tempC'>" & tempC & "<br />" & wDesc & "</div>"  & "<a href='#' class='clrWeather' title='Clear weather information'><img src='images/clrweather.gif' alt='Clear weather information' /></a><div style='clear: both;'> </div></div>"
        'Extended weather
        extWeather = "<div class='extWeather'>"
        set y = objXML.getElementsByTagName("weather")
        for i = 0 to y.length - 1
            for j = 0 to y.item(i).childNodes.length - 1
                select case y.item(i).childNodes(j).nodeName
                    Case "date"
                        wDate = y.item(i).childNodes(j).text 
                        wDate = "<div class='ewDate'>" & WeekdayName(Weekday(wDate), true) & "</div>"
                    Case "weatherDesc"
                        wDesc = "<div class='ewDesc'>" & y.item(i).childNodes(j).text & "</div>"
                    Case "weatherIconUrl"
                        wIcon = "<img class='ewIcon' src=" & y.item(i).childNodes(j).text & " height=20 width=20>"
                    Case "tempMaxC"
                        wHigh = y.item(i).childNodes(j).text & "&deg;"
                    Case "tempMinC"
                        wLow = y.item(i).childNodes(j).text & "&deg;"                   
                end select
            next    
            extWeather = extWeather & "<div class='extCol'>" & wDate & wIcon & "<div class='wHigh'>" & wHigh & " / " & wLow & "</div>" & wDesc & "</div>"
        next
        extWeather = extWeather & "<div style='clear: both;'></div></div>"
        retStr = currentWeather & extWeather
    end if  

    set objXML = nothing    
  set xmlhttp = nothing 

    getweather = retStr

End function

Function getCities 
    getCities = "<option value=-1>Select city for weather info</option>" & _
                            "<optgroup label='ONTARIO'><option value='BARRIE'>BARRIE</option>" & _
                            "<option value='BRAMPTON'>BRAMPTON</option>" & _
                            "<option value='CAMBRIDGE'>CAMBRIDGE</option><option value='HAMILTON'>HAMILTON</option><option value='KINGSTON'>KINGSTON</option><option value='LONDON'>LONDON</option><option value='ORILLIA'>ORILLIA</option><option value='OTTAWA'>OTTAWA</option><option value='OWEN SOUND'>OWEN SOUND</option><option value='PETERBOROUGH'>PETERBOROUGH</option><option value='SOUTH PORCUPINE'>SOUTH PORCUPINE</option><option value='SAINT CATHARINES'>ST. CATHARINES</option><option value='SUDBURY'>SUDBURY</option><option value='THUNDER BAY'>THUNDER BAY</option><option value='TIMMINS'>TIMMINS</option><option value='WINDSOR'>WINDSOR</option><optgroup label='NEWFOUNDLAND & LABRADOR'><option value='CORNER BROOK'>CORNER BROOK</option>"

End Function


%>

1 个答案:

答案 0 :(得分:1)

您有两种选择:将天气数据作为XML获取,自己解析并构建自己的设计,或者操纵原始HTML字符串本身,根据图像名称用您自己的图像替换所需的图像。

我将为第二个选项提供代码示例。首先,检查所有可能的图标的名称(例如,在获得所有图标之前获取具有不同天气的不同城市的天气)并保留它。然后,对于每个图标,获取您自己的图标并将其保存在服务器中,并使用相同的名称。

有了这个,下面的代码应该可以解决这个问题:

Sub ReplaceIcons(ByRef rawHTML)
    Const imagesFolder = "images/"
    Dim arrImageNames, arrTemp, x
    Dim curSrc, y, curImageName
    arrImageNames = Array("black_low_cloud.png", "cloudy_with_light_snow.png")
    arrTemp = GetBetween(rawHTML, "src=", " ")
    For x=0 To UBound(arrTemp)
        curSrc = LCase(arrTemp(x))
        If Right(curSrc, 3)="png" Then
            For y=0 To UBound(arrImageNames)
                curImageName = arrImageNames(y)
                If InStr(curSrc, curImageName)>0 Then
                    rawHTML = Replace(rawHTML, "src=" & curSrc & " ", "src=""" & imagesFolder & curImageName & """ ")
                End If
            Next
        End If
    Next
    Erase arrTemp
End Sub

Function GetBetween(str, leftDelimeter, rightDelimeter)
      Dim tmpArr, result(), x
      tmpArr=Split(str, leftDelimeter)
      If UBound(tmpArr) < 1 Then
          GetBetween=Array() : Exit Function
      End If
      ReDim result(UBound(tmpArr)-1)
      For x=1 To UBound(tmpArr)
          result(x-1)=(Split(tmpArr(x), rightDelimeter))(0)
      Next
      Erase tmpArr
      GetBetween=result
End Function

这只是替换其中两个图标的示例。如果服务器上的图像位于子文件夹中,请将其放在imagesFolder常量中,否则将其留空。在arrImageNames中放置了所有可能的图标。

要使用上述内容,只需在返回HTML之前调用它:

'...
set objXML = nothing    
set xmlhttp = nothing 

Call ReplaceIcons(retStr)

getweather = retStr