【问题标题】:Using VBA To Import XML website into Access使用 VBA 将 XML 网站导入 Access
【发布时间】:2014-02-05 15:06:17
【问题描述】:

我希望每周使用 VBA 从本网站下载汇率 我对 XML 很陌生,一直在查看堆栈交换,并且看到了一些使用表单的实现(我想避免这种方法)

我尝试使用 MS Access 向导导入它,但表中的所有字段都是空白的

如果可能,我想实施这些步骤

  1. 从网页http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml下载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

【问题讨论】:

  • 您可以通过直接从 url 加载 DOMDocument 而不是先获取 XML 然后将该 XML 加载到文档中来简化此任务的开始。执行此操作:xmlDoc.async = False 然后执行此操作:xmlDoc.Load "http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml"
  • 感谢 HansUp,这确实好多了。你知道如何从 XMLDOC 中提取元素吗?

标签: xml ms-access vba


【解决方案1】:

使用 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"))

【讨论】:

  • 非常感谢 Hansup,这正是我想要的。周末愉快
  • 这很棒,并且非常适合该网站;我想对这个网站应用相同的原则:sec.gov/rss/investor/alerts 但它没有 gesmes(我已经用谷歌搜索过,但真的不明白)。如何申请本网站?我想从该站点提取项目、描述、发布日期和链接。非常感谢。
  • "gesmes" 不是 XML 文档的标准功能,所以我认为您最好使用谷歌 XPath。
猜你喜欢
  • 2019-02-06
  • 1970-01-01
  • 2016-11-25
  • 2014-09-19
  • 2021-02-25
  • 2011-11-16
  • 1970-01-01
  • 1970-01-01
  • 2011-03-18
相关资源
最近更新 更多