2016-10-25 49 views
0

我目前收到了一些來自荷蘭的地址數據記錄,這些記錄是正確註冊的,有些是外國的,但沒有正確註冊。VBA檢索Google地理編碼XML ChildNodes

因此,爲了正確地註冊這些地址,我試圖通過使用Google地理編碼API將缺失的記錄添加到數據記錄中。

嘗試從XML記錄中檢索結果時,出現運行時錯誤。 錯誤是:運行時錯誤'-2147467259(80004005)': 預期的令牌'eof'found'='。

//結果/ address_component/LONG_NAME /型號 - > = < --street_name

正如你可以看到我有以便在檢索不同的充的childNodes我需要的困難。

通過工作代碼鏈接到工作簿,您可以在第二個模塊中找到cod。 (https://drive.google.com/open?id=0B1TucCM4DOwydnp5RDg0elJwRDQ

從谷歌API輸出的XML的例子或見下文:

https://developers.google.com/maps/documentation/geocoding/intro

截至目前我得到了以下的代碼:

Option Explicit 

Sub geocode() 

    Dim Request   As New XMLHTTP30 
    Dim Results   As New DOMDocument30 
    Dim StatusNode  As IXMLDOMNode 
    Dim GeoCount  As Integer 
    Dim AutoResult  As Integer 

    For GeoCount = 2 To 10 

     On Error GoTo errorhandler 

     Request.Open "GET", "http://maps.googleapis.com/maps/api/geocode/xml?" _ 
     & "&address=" & Cells(GeoCount, 1) & " " & Cells(GeoCount, 2) & " " & Cells(GeoCount, 3) & "&sensor=false", False 

     Request.send 

     Results.LoadXML Request.responseText 

     Set StatusNode = Results.SelectSingleNode("//status") 


      If UCase(StatusNode.Text) = "OK" Then 
       Cells(GeoCount, 4) = Results.SelectSingleNode("//result/address_component/long_name/type=street_number").Text 
       Cells(GeoCount, 5) = Results.SelectSingleNode("//result/address_component/long_name/type=route").Text 
       Cells(GeoCount, 6) = Results.SelectSingleNode("//result/address_component/long_name/type=postal_code").Text 
       Cells(GeoCount, 7) = Results.SelectSingleNode("//result/address_component/long_name/type=locality").Text 
       Cells(GeoCount, 8) = Results.SelectSingleNode("//result/address_component/long_name/type=country").Text 
       Cells(GeoCount, 9) = Results.SelectSingleNode("//result/geometry/location/lat").Text 
       Cells(GeoCount, 10) = Results.SelectSingleNode("//result/geometry/location/lng").Text 
      Else 

       For AutoResult = 4 To 10 
        Cells(GeoCount, AutoResult) = UCase(StatusNode.Text) 
       Next AutoResult 
      End If 


    Set StatusNode = Nothing 
    Set Results = Nothing 
    Set Request = Nothing 
errorhandler: 
    Set StatusNode = Nothing 
    Set Results = Nothing 
    Set Request = Nothing 
    Next GeoCount 


End Sub 

例如關閉XML退貨:

<GeocodeResponse> 
<status>OK</status> 
<result> 
    <type>street_address</type> 
    <formatted_address>1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA</formatted_address> 
    <address_component> 
    <long_name>1600</long_name> 
    <short_name>1600</short_name> 
    <type>street_number</type> 
    </address_component> 
    <address_component> 
    <long_name>Amphitheatre Pkwy</long_name> 
    <short_name>Amphitheatre Pkwy</short_name> 
    <type>route</type> 
    </address_component> 
    <address_component> 
    <long_name>Mountain View</long_name> 
    <short_name>Mountain View</short_name> 
    <type>locality</type> 
    <type>political</type> 
    </address_component> 
    <address_component> 
    <long_name>San Jose</long_name> 
    <short_name>San Jose</short_name> 
    <type>administrative_area_level_3</type> 
    <type>political</type> 
    </address_component> 
    <address_component> 
    <long_name>Santa Clara</long_name> 
    <short_name>Santa Clara</short_name> 
    <type>administrative_area_level_2</type> 
    <type>political</type> 
    </address_component> 
    <address_component> 
    <long_name>California</long_name> 
    <short_name>CA</short_name> 
    <type>administrative_area_level_1</type> 
    <type>political</type> 
    </address_component> 
    <address_component> 
    <long_name>United States</long_name> 
    <short_name>US</short_name> 
    <type>country</type> 
    <type>political</type> 
    </address_component> 
    <address_component> 
    <long_name>94043</long_name> 
    <short_name>94043</short_name> 
    <type>postal_code</type> 
    </address_component> 
    <geometry> 
    <location> 
    <lat>37.4217550</lat> 
    <lng>-122.0846330</lng> 
    </location> 
    <location_type>ROOFTOP</location_type> 
    <viewport> 
    <southwest> 
    <lat>37.4188514</lat> 
    <lng>-122.0874526</lng> 
    </southwest> 
    <northeast> 
    <lat>37.4251466</lat> 
    <lng>-122.0811574</lng> 
    </northeast> 
    </viewport> 
    </geometry> 
    <place_id>ChIJ2eUgeAK6j4ARbn5u_wAGqWA</place_id> 
</result> 
</GeocodeResponse> 
+0

'type'不是'孩子long_name' –

回答

0

您需要選擇ECT其兄弟的long_name基於斷值type元素

如:

Cells(GeoCount, 4) = Results.SelectSingleNode(_ 
    "//result/address_component[type='street_number']/long_name").Text 
+0

感謝一大堆!管理得到宏的工作。然而,我仍然有第二個問題。 第三和第四個address_component有兩種類型,分別是地方性和政治性。你有沒有機會知道我如何解釋這些「類型」? – Maurice

+0

對不起,我不是一個很大的XPath用戶,所以下一步不是我知道的... –