I need to read an XML document with classic ASP.
The XML file looks like:
<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>
I want to retrieve all application
tags and store them into a Dictionnary. The key is the application name and the value is the link attribute.
Here is my code (corrected with the very good replies I got):
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)
However, xmlAppCollection.Count
returns zero.
What do I miss?
UPDATE 1:
Now I've got the following error:
Object doesn't support this property or method: 'app.Value'
.
The problem comes from xmlAppCollection.Add app.Value, app.Attributes.GetNamedItem("link")
UPDATE 2:
I hope this is the last question. I want to loop over the dictionary:
' 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
But I've got the error message: Object doesn't support this property or method
on the line If xmlAppCollection.Exists(member) = True And xmlAppCollection.Item(member) <> "" Then
. It seems to come from the Item
property.
Best Answer
Not shure if i'm right, but the selector should start with / if its root, or // if anywhere, also the DocumentElement part not needed.
so try using
Set xmlApp = xmlObj.SelectNodes("//application")
or
Set xmlApp = xmlObj.SelectNodes("/config/applications/application")
also, add some debog response.write to your for each loop, so during development you can see if it's actually doing anything.
For ref click here.