我正在尝试从 Google 的 Map API 获取纬度/经度值。我在 Excel 中创建 IE 对象并打开浏览器没有任何问题,但我在导航返回的 XML 文档以获取我需要的值时遇到了麻烦。
我已经创建了 HTMLDocument,并且该引用似乎是有效的,但我不知道如何导航它。有没有办法获取整个 HTML 输出的字符串?从中我可以得到我需要的东西。
使用 MSXML - 您需要在 Excel VBA IDE 的工具/参考中引用 Microsoft XML。
您可以从文件 ( Load
) 或字符串 ( LoadXML
) 加载 XML。在我的示例中,我从文件加载:
<?xml version="1.0" encoding="utf-8"?>
<testxml>
<tests>
<test nr="1">Number 1</test>
<test nr="2">Number 2</test>
<test nr="3">Number 3</test>
<test nr="4">Number 4</test>
</tests>
</testxml>
VBA 加载文件:
Option Explicit
Sub TestXML()
'***** Declare document
Dim oXmlDoc As DOMDocument
Dim bRet As Boolean
'***** Create empty document
Set oXmlDoc = New DOMDocument
'***** Load XML from file
bRet = oXmlDoc.Load("c:\temp\test.xml")
'***** Manipulate XML
MsgBox oXmlDoc.Text
MsgBox oXmlDoc.XML
MsgBox oXmlDoc.SelectSingleNode("//testxml/tests/test[@nr='3']").Text
'***** Clean up
Set oXmlDoc = Nothing
End Sub