2013-07-16 2 views
1

Excel에서 OpenStreetMap 여행 시간을 사용하여 거리 매트릭스를 작성하려고합니다. YOURS API 및 Google Maps API 엑셀 모듈을 참조 (http://oco-carbon.com/2012/05/17/a-google-maps-journey-time-function-for-excel/)로 사용하고 있습니다. http://www.yournavigation.org/api/1.0/gosmore.php?format=xml&flat=60.480398&flon=22.277206&tlat=60.402923&tlon=22.355558&v=motorcar&fast=1&layer=mapnikExcel의 XML 네임 스페이스

현재 내 VB 코드는 다음과 같습니다 :

내가 Excel로 가지고 노력하고있어 XML의 예입니다

:

Function G_TIME(Flat As String, Flon As String, Tlat As String, Tlon As String) As Double 
' Requires a reference to Microsoft XML, v6.0 
' Draws on the stackoverflow answer at bit.ly/parseXML 
Dim myRequest As XMLHTTP60 
Dim myDomDoc As DOMDocument60 
Dim timeNode As IXMLDOMNode 

G_TIME = 0 
On Error GoTo exitRoute 
' Check and clean inputs 
' Origin = Replace(Origin, " ", "%20") 
' Destination = Replace(Destination, " ", "%20") 
' Read the XML data from the Google Maps API 
Set myRequest = New XMLHTTP60 
' myRequest.Open "GET", "http://maps.googleapis.com/maps/api/directions/xml?origin=" _ 
' & Origin & "&destination=" & Destination & "&sensor=false", False 
myRequest.Open "GET", "http://www.yournavigation.org/api/1.0/gosmore.php?format=kml&flat=" & Flat & "&flon=" & Flon & "&tlat=" & Tlat & "&tlon=" & Tlon & "&v=motorcar&fast=1&layer=mapnik", False 
myRequest.Send 
' Make the XML readable usign XPath 
Set myDomDoc = New DOMDocument60 
myDomDoc.LoadXML myRequest.responseText 
' Get the time node value 
Set timeNode = myDomDoc.SelectSingleNode("//Document/traveltime") 
'If Format = "Decimal" Then ' Return as a decimal - 30 mins as 0.5 hrs 
' G_TIME = timeNode.Text ' Seconds in an hour 
'Else 'Return in Excel's 00:00:00 date format - 30 mins as 00:30:00 
    G_TIME = timeNode.Text ' Seconds in a day 
'End If 
exitRoute: 
' Tidy up 
Set timeNode = Nothing 
Set myDomDoc = Nothing 
Set myRequest = Nothing 
End Function 

나는 문제가 여기에있다 생각

Set timeNode = myDomDoc.SelectSingleNode("//Document/traveltime") 

YOURS XML은 Google 어스 KML 스타일을 사용하며 Excel 용으로 정의해야합니다. XmlNamespaceManager를 사용하려고 시도했지만 작동하지 않습니다. 뭔가를 가져올 필요가 있지만 VB 용 Excel에 익숙하지 않아 어디에서해야할지 모르겠습니다.

도움을 주시면 감사하겠습니다.

답변

0

Microsoft의 DOMDocuments에서 XPath 쿼리를 사용할 때 기본 네임 스페이스에 문제가 있습니다. 당신은 기본 네임 스페이스를 접두사를 제공하고 XPath 쿼리에 해당 접두어를 사용할 필요가 -이 같은 :

Set myDomDoc = New DOMDocument60 
myDomDoc.loadXML myRequest.responseText 
myDomDoc.setProperty "SelectionNamespaces", "xmlns:r='http://earth.google.com/kml/2.0'" 

' Get the time node value 
Set timeNode = myDomDoc.selectSingleNode("//r:Document/r:traveltime") 
이에

더 많은 정보를 찾을 수 here