如何将XML中的特殊字符转换为VBA中的可读字符串?

时间:2015-07-09 21:50:36

标签: xml excel vba

我有以下代码,用于ping Yahoo Finance,并下载特定安全/股票的个人资料:

Public Function getETFDetails(theSymbol As String)
    Dim xmlhttp As Object
    Dim strURL As String
    Dim x As String
    Dim sSearch As String, myDIV As String, theDeets As String
    'theSymbol = "SPY"

    strURL = "http://finance.yahoo.com/q/pr?s=" & theSymbol & "+Profile"
    DeleteUrlCacheEntry (strURL)
    Set xmlhttp = CreateObject("msxml2.xmlhttp")
    With xmlhttp
        .Open "get", strURL, False
        .send
        x = .responsetext
    End With
    Set xmlhttp = Nothing

    sSearch = "Fund Summary"
    myDIV = Mid(x, InStr(1, x, sSearch) + Len(sSearch))
    sSearch = "<td>"
    myDIV = Mid(myDIV, InStr(1, myDIV, sSearch) + Len(sSearch))
    theDeets = Left(myDIV, InStr(1, myDIV, "</td>") - 1)

    getETFDetails = theDeets

End Function

这很好用,我得到了很好的结果。但是,如果结果包含特殊字符(例如,S&amp; P500 ETF,&#39; SPY&#39;),我会得到以下结果:

The investment seeks to provide investment results that, before expenses, 
generally correspond to the price and yield performance of the S&amp;P 500® Index.

&符号和商标的处理是我的问题。如何从结果中取出它们?我完全删除它们只是让它说'SP 500&#39;我很好,但我希望有更强大的功能,然后通过一些字符列表(字符串)手动删除它们。有什么想法吗? (我尝试过使用StrConv函数,但无论出于何种原因,它都只返回字符串的第一个字符。)

1 个答案:

答案 0 :(得分:2)

没有内置的&#34; HTML / XML解码&#34;在VBA中运行,但您可以将字符串写入XML文档并回读文本。例如:

Set doc = CreateObject("Msxml2.DOMDocument.6.0")
strXML = "S&amp;P 500"
doc.LoadXML "<root>" & strXML & "</root>"
strDecoded = doc.Text