2014-02-05 206 views
3

我希望每周使用VBA从本网站下载汇率 我对XML非常陌生,并且一直在关注堆栈交换,并且已经看到一些使用形式(我想避免这种方法)使用VBA将XML网站导入Access

我曾尝试使用的MS Access向导,但表中的所有字段为空将其导入

我想执行这些步骤,如果可能的

  1. 从网页下载XML http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml
  2. 循环遍历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 
+1

您可以通过直接从url加载DOMDocument来简化此任务的开始,而不是首先获取XML,然后将该XML加载到文档中。做到这一点:'xmlDoc.async = False'然后这个:'xmlDoc.Load“http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml”' – HansUp

+0

感谢HansUp,这确实很远更好。你有什么想法如何从XMLDOC中提取元素? –

回答

3

使用XPath选择所需的元素,然后getAttribute到提取每个选定元素的currencyrate属性的值。

Const cstrXPath As String = "/gesmes:Envelope/Cube/Cube/Cube" 
Dim xmlDoc As MSXML2.DOMDocument 
Dim xmlElement As MSXML2.IXMLDOMElement 
Dim xmlSelection As MSXML2.IXMLDOMSelection 
Dim i As Long 
Dim strUrl As String 

strUrl = "http://www.ecb.europa.eu/stats/" & _ 
    "eurofxref/eurofxref-daily.xml" 

Set xmlDoc = New MSXML2.DOMDocument 
xmlDoc.async = False 
xmlDoc.Load strUrl 

Set xmlSelection = xmlDoc.SelectNodes(cstrXPath) 
Debug.Print "xmlSelection.Length: " & xmlSelection.Length 
i = 1 
For Each xmlElement In xmlSelection 
    Debug.Print i, xmlElement.getAttribute("currency"), _ 
     xmlElement.getAttribute("rate") 
    i = i + 1 
Next xmlElement 

您可以查看立即窗口中输出;你可以使用Ctrl + g去那里。下面是一个缩略的输出样本...

xmlSelection.Length: 32 
1   USD   1.3495 
2   JPY   136.93 
3   BGN   1.9558 

最终要存储这些值,而不仅仅是Debug.Print他们。当你到达这一点时,请注意getAttribute返回文本值。如果您将在数字字段中存储rate,例如。单一,您可以在存储它时将文本值转换为数字。

CSng(xmlElement.getAttribute("rate")) 
+0

非常感谢Hansup,这正是我所期待的。周末愉快 –