Excel中的XML名称空间

我试图在Excel中使用OpenStreetMap旅行时间来构build距离matrix。 我使用YOURS API和Google Maps API excel模块作为参考( http://oco-carbon.com/2012/05/17/a-google-maps-journey-time-function-for-excel/ ) 。

这是我想要带入Excel的XML示例: http : //www.yournavigation.org/api/1.0/gosmore.php? format = xml&flat=60.480398&flon=22.277206& tlat= 60.402923& tlon= 22.355558& v=汽车及快= 1&层= Mapnik的

目前我的VB代码如下所示:

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但我无法得到它的工作。 我想我需要导入的东西,但我不熟悉Excel的VB,所以我不知道该怎么做。

任何帮助表示赞赏!

与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") 

更多信息可以在这里find