我需要用经典的 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
标签并将它们存储到字典中。键是应用程序名称,值是链接属性。
这是我的代码(用我得到的非常好的回复更正):
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
财产。