使用经典ASP(VBscript)解析XML的问题

时间:2015-04-26 12:12:31

标签: vbscript asp-classic xml-parsing

我有以下代码,过去工作得很完美,但现在由于某些原因,没有。

我正在阅读的XML位于:https://forex.boi.org.il/currency.xml

以下代码应解析XMl,然后保存USD / ILS汇率。正如我所说,它已经不存在了,我无法弄清楚什么是错的。

forexURL = "https://forex.boi.org.il/currency.xml"

getUSDRate = 0 
MyRate = 0 

Set xmlObj = Server.CreateObject("MSXML2.FreeThreadedDOMDocument")
xmlObj.async = False
xmlObj.setProperty "ServerHTTPRequest", True
xmlObj.Load(forexURL)
Set xmlList = xmlObj.getElementsByTagName("CURRENCY")
Set xmlObj = Nothing
x = 1
For Each xmlItem In xmlList

    response.write "<p>" &  xmlItem.childNodes(0).text
    response.write "<p>" &    xmlItem.childNodes(1).text
    response.write "<p>" &   xmlItem.childNodes(2).text
    response.write "<p>" &   xmlItem.childNodes(3).text
    response.write "<p>" &   xmlItem.childNodes(4).text
    response.write "<p>" &   xmlItem.childNodes(5).text
    response.write "<p>___________________<br />" & x & "</p>"

    if xmlItem.childNodes(2).text = "USD" then 
        MyRate = xmlItem.childNodes(4).text
        exit for
    end if 

    x = x +1

Next
Set xmlList = Nothing

2 个答案:

答案 0 :(得分:3)

我刚在我的机器上试过这个。尝试替换

Server.CreateObject("MSXML2.FreeThreadedDOMDocument")

Server.CreateObject("Msxml2.DomDocument.6.0")

修改

Server.CreateObject("MSXML2.FreeThreadedDOMDocument.6.0") 

似乎也有效

答案 1 :(得分:3)

我怀疑(疯狂猜测)改变服务器端处理SSL的方式是导致问题的原因。也许他们为了应对最近的SSL漏洞而禁用了旧的,更不安全的密码。

与@John相同 - 当您从MSXML2.FreeThreadedDOMDocument(加载版本MSXML2版本3)更改为显式加载更现代版本6(MSXML2.FreeThreadedDOMDocument.6.0)时,文档下载成功。

话虽如此,我已经对您的代码进行了一些更改,主要是为了更具可读性,并且当文档加载由于某种原因而失败时,它会明显失败。

请注意

  • 使用XPath
  • 辅助函数GetText()代替盲目索引到子节点
  • parseError检查以使LoadXmlDocument非静默地失败

Option Explicit

Dim usdRate, x, currencies, curr

Set currencies = LoadXmlDocument("https://forex.boi.org.il/currency.xml")
usdRate = GetText(currencies, "//CURRENCY[CURRENCYCODE = 'USD']/RATE")

x = 1
For Each curr In currencies.getElementsByTagName("CURRENCY")
    Response.Write "<p>" & GetText(curr, "NAME") & "</p>"
    Response.Write "<p>" & GetText(curr, "UNIT") & "</p>"
    Response.Write "<p>" & GetText(curr, "CURRENCYCODE") & "</p>"
    Response.Write "<p>" & GetText(curr, "COUNTRY") & "</p>"
    Response.Write "<p>" & GetText(curr, "RATE") & "</p>"
    Response.Write "<p>" & GetText(curr, "CHANGE") & "</p>"
    Response.Write "<p>___________________<br />" & x & "</p>"
    x = x + 1
Next
' ----------------------------------------------------------------------

' loads an XML document from a URL and returns it
Function LoadXmlDocument(url)
    Set LoadXmlDocument = CreateObject("MSXML2.FreeThreadedDOMDocument.6.0")
    LoadXmlDocument.async = False
    LoadXmlDocument.setProperty "ServerHTTPRequest", True
    LoadXmlDocument.setProperty "SelectionLanguage", "XPath"
    LoadXmlDocument.Load url
    If LoadXmlDocument.parseError <> 0 Then
        Err.Raise vbObjectError + 1, _
            "LoadXmlDocument", _
            "Cannot load " & url & " (" & LoadXmlDocument.parseError.reason & ")"
    End If
End Function

' finds the first node that matches the XPath and returns its text value
Function GetText(context, xpath)
    Dim node
    Set node = context.selectSingleNode(xpath)
    If node Is Nothing Then
        GetText = vbEmpty
    Else
        GetText = node.text
    End If
End Function