2013-04-15 106 views
0

我試圖用谷歌地圖API定義2點之間的駕駛距離。它返回一個我加載到XMLDOM對象中的XML。然而,我似乎無法找到任何地方,我可以如何獲得兒童內兒童的價值......我已閱讀微軟教程,但並未顯示如何從XML中檢索值。經典的ASP讀取XML值

一個例子XML是:

<DistanceMatrixResponse> 
<status>OK</status> 
<origin_address>Gentsesteenweg 5, 9200 Dendermonde, België</origin_address> 
<destination_address>Veerstraat 39, 9200 Dendermonde, België</destination_address> 
<row> 
    <element> 
     <status>OK</status> 
     <duration> 
      <value>117</value> 
      <text>2 min.</text> 
     </duration> 
     <distance> 
      <value>1061</value> 
      <text>1,1 km</text> 
     </distance> 
    </element> 
</row> 
</DistanceMatrixResponse> 

我的代碼的部分是:

Set XmlObj = Server.CreateObject("Microsoft.XMLHTTP") 
strURL = "http://maps.googleapis.com/maps/api/distancematrix/xml?origins=" & dblFromLatitude & "+" & dblFromLongitude & "&destinations=" & dblToLatitude & "+" & dblToLongitude & "&mode=driving&language=" & strLanguage & "&sensor=false" 
'DebugFile strURL 
XmlObj.Open "POST", strURL, false 
XmlObj.send  
strResponse = XmlObj.responseText 
Set XmlObj = Nothing 


Set xmlLongLat = Server.CreateObject("Microsoft.XMLDOM") 
xmlLongLat.async=false 
xmlLongLat.loadXML(strResponse) 

If Not xmlLongLat.GetElementsByTagName("DistanceMatrixResponse")(0).ChildNodes(0).GetElementsByTagName("row")(0).ChildNodes(0).GetElementsByTagName("element")(0).ChildNodes(0).GetElementsByTagName("distance")(0).ChildNodes(0) Is Nothing Then 

    dblDrivingDistance = Round(CDbl(Nz(xxmlLongLat.GetElementsByTagName("DistanceMatrixResponse")(0).ChildNodes(0).GetElementsByTagName("row")(0).ChildNodes(0).GetElementsByTagName("element")(0).ChildNodes(0).GetElementsByTagName("distance")(0).ChildNodes(0).NodeValue, 0))/1000,2) 
Else 'xmlLongLat.GetElementsByTagName("element")(0) Is Nothing Then 
    dblDrivingDistance = 0 
    DebugFile "fgfzefzef" 
End If 'xmlLongLat.GetElementsByTagName("element")(0) Is Nothing Then 

Set xmlLongLat = Nothing  

我想要得到的「價值」的「距離」,但我不覺得是公正儘管嘗試過一切

+0

您是否已經嘗試過「分步」方法?爲什麼不把響應存儲到變量中? – reporter

回答

0
xmlDoc.setProperty "SelectionLanguage", "XPath" 
node = xmlDoc.selectSingleNode("/row/elelemnt/distance/value").innerText 

或類似成功 - 與玩耍他XPath

+0

添加 「xmlLongLat.setProperty 」SelectionLanguage「, 」的XPath「 \t集節點= xmlLongLat.selectSingleNode(」/ DistanceMatrixResponse的/行/元件/距離/值 「) \t dblDrivingDistance = NZ(node.text,0)」 之後loadXML在最後一行返回一個「Object required:'node'」錯誤。看起來像XML永遠不會加載或什麼 –

+0

我試圖查詢谷歌地圖駕駛距離api,但它似乎我已經使用了我所有的日常查詢,這就是爲什麼它沒有工作。應該是「node = xmlDoc.selectSingleNode(」/ DistanceMatrixResponse/row/elelemnt/distance/value「)。text」to be correct –