我需要閱讀經典的ASP文檔。閱讀XML文件的經典ASP
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
但我得到的錯誤消息:上線If xmlAppCollection.Exists(member) = True And xmlAppCollection.Item(member) <> "" Then
Object doesn't support this property or method
。它似乎來自Item
property。
Re。最後一個問題:將該行分成兩部分,看看問題是來自「Exists()」還是「Item」。請注意,VBScript中的'And'不會短路(如果我沒有記錯的話),所以即使'Exists()'返回'false'也會調用Item'。 – MiMo
感謝您的回覆。好吧,我記住'和'不會使情況短路。我已經嘗試拆分線。這個問題絕對來自'Item'。 – Maxbester