2013-04-15 116 views
1

我需要閱讀經典的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) <> "" ThenObject doesn't support this property or method。它似乎來自Itemproperty

+0

Re。最後一個問題:將該行分成兩部分,看看問題是來自「Exists()」還是「Item」。請注意,VBScript中的'And'不會短路(如果我沒有記錯的話),所以即使'Exists()'返回'false'也會調用Item'。 – MiMo

+0

感謝您的回覆。好吧,我記住'和'不會使情況短路。我已經嘗試拆分線。這個問題絕對來自'Item'。 – Maxbester

回答

1

我說你應該使用app.text,而不是比app.Value。見here

+0

確實如此。謝謝 – Maxbester

+0

我已更新我的問題。你有什麼錯誤的想法嗎? – Maxbester

+0

您不斷更改添加新內容的問題 - 而不是將任何內容標記爲答案。你應該創造新的問題。 – MiMo

2

如果我是正確的,不要確定,但選擇器應該以/開頭,如果它的根,或者//在任何地方,也不需要DocumentElement部分。

所以嘗試使用

集xmlApp = xmlObj.SelectNodes( 「//申請」)

集xmlApp = xmlObj.SelectNodes( 「/配置/應用/應用」)

另外,爲每個循環添加一些debog response.write,以便在開發過程中可以看到它是否真的在做任何事情。

For ref click here.

+0

你一定是對的。我有一個新的錯誤:'對象不支持這個屬性或方法:'app.Value''。問題來自'xmlAppCollection.Add app.Value,app.Attributes.GetNamedItem(「link」)' – Maxbester