读取XML文件经典ASP

时间:2013-04-15 13:34:52

标签: xml asp-classic vbscript

我需要阅读带有经典ASP的XML文档。

XML文件如下所示:

<config>
    ...
    <applications>
        <application id="1" link="http://office.microsoft.com/en-001/word-help/what-s-new-in-word-2013-HA102809597.aspx">Word 2013</application>
        <application id="2" link="http://office.microsoft.com/en-001/excel-help/what-s-new-in-excel-2013-HA102809308.aspx">Excel 2013</application>
        ...
    </applications>
    ...
</config>

我想检索所有application标记并将它们存储到Dictionnary中。关键是应用程序名称,值是链接属性。

这是我的代码(通过我收到的非常好的回复更正):

Set xmlObj = Server.CreateObject("Microsoft.XMLDOM")
xmlObj.async = False
xmlObj.load(Server.MapPath("/SomePath/Applications.xml"))

' List of application nodes
Dim xmlApp : Set xmlApp = xmlObj.DocumentElement.SelectNodes("/config/applications/application")

' Dictionnary of applications
Dim xmlAppCollection : Set xmlAppCollection = CreateObject("Scripting.Dictionary")
' Single application node
Dim app

For Each app In xmlApp
    ' Populate the application dictionnary
    If app.Attributes.GetNamedItem("link") Is Nothing Then
        xmlAppCollection.Add app.Text, ""
    Else
        xmlAppCollection.Add app.Text, app.Attributes.GetNamedItem("link")
    End If
Next

Response.write("Count: "&xmlAppCollection.Count)

但是,xmlAppCollection.Count返回零。

我想念什么?


更新1:

现在我遇到以下错误: Object doesn't support this property or method: 'app.Value'

问题来自xmlAppCollection.Add app.Value, app.Attributes.GetNamedItem("link")


更新2:

我希望这是最后一个问题。我想循环遍历字典:

' applications is an array of application names
For Each member In applications
    ' Test that the software exists in the dictionary and the link is not empty
    If xmlAppCollection.Exists(member) = True And xmlAppCollection.Item(member) <> "" Then
        Response.write("<tr><th>Software</th><td><a href='" & xmlAppCollection.Item(member) & "'>" & member & "</a></td></tr>")
    Else
        Response.write("<tr><th>Software</th><td>"&member&"</td></tr>")
    End If
Next

但是我在Object doesn't support this property or method行上收到了错误消息:If xmlAppCollection.Exists(member) = True And xmlAppCollection.Item(member) <> "" Then。它似乎来自Item property

2 个答案:

答案 0 :(得分:2)

如果我是对的,那么不是shure,但是选择器应该以/如果它的根开头,或者//如果在任何地方,也不需要DocumentElement部分。

所以尝试使用

设置xmlApp = xmlObj.SelectNodes(“// application”)

设置xmlApp = xmlObj.SelectNodes(“/ config / applications / application”)

另外,为每个循环添加一些debog response.write,因此在开发过程中你可以看到它是否真的在做任何事情。

For ref click here.

答案 1 :(得分:1)

我说你应该使用app.text而不是app.Value。请参阅here