将XML读入经典的asp

时间:2014-06-18 09:34:21

标签: xml asp-classic

我是编程的新手,也是XML的新手,我知道这没有帮助......

我需要读取一个看起来像这样的外部XML文件

 <?xml version="1.0" encoding="UTF-8"?>
 <gesmes:Envelope xmlns:gesmes="http://www.gesmes.org/xml/2002-08-01" xmlns="http://www.ecb.int /vocabulary/2002-08-01/eurofxref">
<gesmes:subject>Reference rates</gesmes:subject>
<gesmes:Sender>
<gesmes:name>European Central Bank</gesmes:name>
</gesmes:Sender>
<Cube>
<Cube time='2014-06-17'>
  <Cube currency='USD' rate='1.3568'/>
  <Cube currency='JPY' rate='138.40'/>
  <Cube currency='BGN' rate='1.9558'/>
  <Cube currency='CZK' rate='27.445'/>

在ASP页面中,我想阅读并显示两种货币及其值,但我没有找到任何看起来像这样的例子。

提前谢谢, 的Jeroen

2 个答案:

答案 0 :(得分:1)

-

    Set oXML = Server.CreateObject("MSXML2.DomDocument.4.0")
    oXML.LoadXML(sXML) ' sXML is a variable containing the content of your XML file

    For Each oNode In oXML.SelectNodes("/Cube/cube")
        sCurrency= oNode.GetAttribute("currency")
        sRate= oNode.GetAttribute("rate")

       Do something with these values here
    Next

    Set oXML = Nothing

答案 1 :(得分:1)

我使用以下代码

Sub DisplayRates    

 URL = "http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml"
 'URL = "localhost/wallboard/ratesxml.xml"

Set oXML = Server.CreateObject("MSXML2.DomDocument.4.0")
oXML.setProperty "ServerHTTPRequest" , True
oXML.async = False
oXML.LoadXML URL

For Each oNode In oXML.SelectNodes("/Cube/cube")
    sCurrency   = oNode.GetAttribute("currency")
    sRate       = oNode.GetAttribute("rate")

' set itemList = oXML.SelectNodes ("/Cube/cube")

    'For Each itemAttrib In itemList
    'sCurrency = itemAttrib.SelectSingleNode ("currency").text
    'sRate = itemAttrib.SelectSingleNode ("rate").text




 Response.Write "<TD ALIGN='CENTER' BGCOLOR='" & TableColor_CSQStats & "'><FONT SIZE =' " & TextSize_CSQStats & " ' COLOR='" & TextColor & "'>" & sCurrency & "</FONT></TD>" 
 Response.Write "<TD ALIGN='CENTER' BGCOLOR='" & TableColor_CSQStats & "'><FONT SIZE =' " & TextSize_CSQStats & " ' COLOR='" & TextColor & "'>"& sRate & "</FONT></TD>" 


Next

Set oXML = Nothing

 End Sub

字段未填充,使用本地文件或远程文件没有区别。页面本身不会产生任何错误,并且正常加载。

感谢您对此进行调查:-) 的Jeroen