【发布时间】:2014-02-05 15:06:17
【问题描述】:
我希望每周使用 VBA 从本网站下载汇率 我对 XML 很陌生,一直在查看堆栈交换,并且看到了一些使用表单的实现(我想避免这种方法)
我尝试使用 MS Access 向导导入它,但表中的所有字段都是空白的
如果可能,我想实施这些步骤
- 从网页http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml下载XML
- 循环遍历 XML 并将货币和汇率放入新的或现有的两列表中
目前我有以下代码。但它显然是基于其他人的工作而组合在一起的,并且比其他任何东西都更像是一个工作模板 谁能指出我正确的方向
Sub Test()
'**********************************************************
' DOWNLOAD XML DATA
' ref: http://stackoverflow.com/questions/7091162/access-vba-how-to-download-xml-file- and-enter-its-data-into-a-recordset
'**********************************************************
Dim obj As MSXML2.ServerXMLHTTP
Set obj = New MSXML2.ServerXMLHTTP
obj.Open "GET", "http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml", False
'in case you are sending a form *POST* or XML data to a SOAP server set content type
obj.setRequestHeader "Content-Type", "text/xml"
obj.send
Dim status As Integer
status = obj.status
If status >= 400 And status <= 599 Then
Debug.Print "Error Occurred : " & obj.status & " - " & obj.statusText
End If
'**********************************************************
'CREATE XML DOM DOCUMENT
'**********************************************************
Dim xmlDoc As MSXML2.DOMDocument
Dim xmlElement As MSXML2.IXMLDOMElement
Dim xmlNode As MSXML2.IXMLDOMElement
Set xmlDoc = New MSXML2.DOMDocument
xmlDoc.loadXML (obj.responseText)
'**********************************************************
'ACCESS ROWS
'http://stackoverflow.com/questions/11305/how-to-parse-xml-in-vba
'**********************************************************
Dim point As IXMLDOMNode
Set point = xmlDoc.firstChild
Debug.Print point.selectSingleNode("subject").Text
End Sub
【问题讨论】:
-
您可以通过直接从 url 加载
DOMDocument而不是先获取 XML 然后将该 XML 加载到文档中来简化此任务的开始。执行此操作:xmlDoc.async = False然后执行此操作:xmlDoc.Load "http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml" -
感谢 HansUp,这确实好多了。你知道如何从 XMLDOC 中提取元素吗?