使用VBA请求在线XML的特定属性

时间:2015-05-30 21:53:21

标签: xml excel vba

最近我试图在VBA中编写一个自定义函数,用于从Web服务获取股票信息。但是,生成的XML编写如下:

<ComportamentoPapeis>
<Papel Codigo="BOVA11" Nome="ISHARES BOVA CI" 
       Ibovespa="" Data="29/05/2015 17:29:57" 
       Abertura="52,04" Minimo="51,28" Maximo="52,24" 
       Medio="51,64" Ultimo="51,35" Oscilacao="-1,98"/>
</ComportamentoPapeis>

我尝试编写以下代码,但是当我运行它时,它表示找不到方法或数据。

    Function STOCK(sName As String, sItem As String, Optional sURL = "") As Variant
    Dim oHttp As New MSXML2.XMLHTTP60
    Dim xmlResp As MSXML2.DOMDocument60
    Dim result As Variant
    On Error GoTo EH


    If sURL = "" Then
        sURL = "http://www.bmfbovespa.com.br/Pregao-" & _
                "Online/ExecutaAcaoAjax.asp?CodigoPapel="
    End If

    'open the request and send it
    oHttp.Open "GET", sURL & sName, False
    oHttp.Send

    'get the response as xml
    Set xmlResp = oHttp.responseXML

    ' get Item
    STOCK = xmlResp.SelectSingleNode("/ComportamentoPapeis/Papel"). _
                 Atrributes.getNamedItem(sItem).Text


    ' Examine output of these in the Immediate window
    Debug.Print sName
    Debug.Print xmlResp.XML

CleanUp:
    On Error Resume Next
    Set xmlResp = Nothing
    Set oHttp = Nothing
Exit Function
EH:
    STOCK = CVErr(xlErrValue)
    GoTo CleanUp
End Function

在这种特殊情况下,sName = BOVA11和sItem = Nome

有人知道出了什么问题吗?我已将它引用到MSXML v6.0

1 个答案:

答案 0 :(得分:0)

Atrributes.getNamedItem(sItem).Text - Attributes

中有拼写错误