【发布时间】:2014-08-16 02:54:22
【问题描述】:
我有一个电子表格,其中包含 A 列中的城市,州。我想从当天到 B 列中获取 3 天的沉淀类型/值。我开始使用 openweathermap API,但我发现我缺乏经验XML 正在杀死我。以下是我的开始方式,但我无法走得更远。我为使用节点和元素所做的任何尝试都没有成功。任何帮助将不胜感激。
这里是一个示例 XML 的链接 http://api.openweathermap.org/data/2.5/forecast/daily?q=Boston+MA&mode=xml&units=metric&cnt=7?
Option Explicit
Function CityForecast(City As String) As String
Dim StartingURL As String
Dim SecondaryURL As String
Dim FinalURL As String
Dim CorrectedCity As String
Dim objXML As Object
Dim objDOM As MSXML2.DOMDocument60
CorrectedCity = Replace(City, " ", "+")
CorrectedCity = Replace(City, ",", "+")
StartingURL = "http://api.openweathermap.org/data/2.5/forecast/daily?q="
SecondaryURL = "&mode=xml&units=metric&cnt=7"
FinalURL = StartingURL + CorrectedCity + SecondaryURL
Debug.Print FinalURL
Set objXML = CreateObject("MSXML2.XMLHTTP")
Set objDOM = New MSXML2.DOMDocument60
With objXML
.Open "GET", FinalURL, False
.setRequestHeader "Content-Type", "application/x-www-form-URLEncoded"
.send
objDOM.LoadXML .responseText
End With
End Function
Function CorrectedDate(WxDate As String) As String
Dim yr As Integer
Dim dy As Integer
Dim mnth As Integer
yr = Left(WxDate, 4)
mnth = Right(Left(WxDate, 7), 2)
dy = Right(WxDate, 2)
CorrectedDate = mnth & "/" & dy & "/" & yr
End Function
【问题讨论】:
-
使用 xpath 应该很容易,我想...给我一些测试一下。
标签: xml vba excel openweathermap