簡短的回答是XPath - 值得學習,如果你打算使用任何類型的XML
在Excel的宏編輯器的工作,去工具>引用,並添加引用「微軟XML,V6.0」現在插入>模塊並添加以下代碼:
Sub getDistances()
Dim xhrRequest As XMLHTTP60
Dim domDoc As DOMDocument60
Dim ixnlDistanceNodes As IXMLDOMNodeList
Dim ixnNode As IXMLDOMNode
Dim lOutputRow As Long
' Read the data from the website
Set xhrRequest = New XMLHTTP60
xhrRequest.Open "GET", "http://maps.googleapis.com/maps/api/directions/xml?origin=Scoresby&destination=Melborne&sensor=false", False
xhrRequest.send
' Copy the results into a format we can manipulate with XPath
Set domDoc = New DOMDocument60
domDoc.loadXML xhrRequest.responseText
' The important bit: select every node called "value" which is the child of a node called "distance" which is
' in turn the child of a node called "step"
Set ixnlDistanceNodes = domDoc.selectNodes("//step/distance/value")
' Basic stuff to output the distances
lOutputRow = 1
With Worksheets("Sheet1")
.UsedRange.ClearContents
For Each ixnNode In ixnlDistanceNodes
.Cells(lOutputRow, 1).Value = ixnNode.Text
lOutputRow = lOutputRow + 1
Next ixnNode
End With
Set ixnNode = Nothing
Set ixnlDistanceNodes = Nothing
Set domDoc = Nothing
Set xhrRequest = Nothing
End Sub
要擴大這種覆蓋多個車次你只需通過必要的起源和循環目的地,將每一對作爲參數傳遞給此過程,然後以您需要的任何格式輸出結果
非常好的例子。正如提醒一樣(並且我不想成爲吸食者):使用Google地圖API時也需要您顯示Google地圖(「路線API只能與顯示結果一起使用谷歌地圖「,http://code.google.com/intl/zh-CN/apis/maps/documentation/directions/#Limits),儘管在這個特定的例子中,我無法看到它是如何以有用的方式完成的。只是在說。 – 2011-05-25 10:07:35