• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 205
  • Last Modified:

Simple ASP variable question (bit of XML, not much though!)

Hi all,Im trying to do something pretty basic, read a set of values from an xml file and set variables depending not them, I can read the xml file perfectly, but cant work out how to set and name variables based on them. Can anyone help?

Here is the script I have so far:
<%

Set xmlDOC = Server.CreateObject("Microsoft.XMLDOM")
xmlDoc.async = False
'load xml file
xmlDOC.Load(Server.MapPath("note.xml"))

for each x in xmlDoc.documentElement.childNodes

response.write(x.nodeName & "<br />")
response.write(x.childnodes(0).nodeValue & "<br />")

next
%>

and here is the xml

<?xml version="1.0"?>
<note time="12:03:46">
  <to>Tove</to>
  <from>Jani</from>
  <heading>Reminder</heading>
  <body>Don't forget me this weekend!</body>
</note>


So in the case above it would create the variable "to" and set the value to "Tove", and the variable "from" and set it to "Jani" etc, basically more variables can be added and removed from the XML file.

Can anyone help?

Alicia



0
fox_statton
Asked:
fox_statton
1 Solution
 
stezisCommented:
You can try to use the session-variables collection, like:

<%

Set xmlDoc = Server.CreateObject("Microsoft.XMLDOM")
xmlDoc.async = False
'load xml file
xmlDoc.Load(Server.MapPath("1.xml"))
dim varCollection
for each x in xmlDoc.documentElement.childNodes
     session(x.nodeName) =  x.text
next

Response.Write session("to")
Response.Write session("from")
Response.Write session("heading")
Response.Write session("body")

%>
0
 
AlfaNoMoreCommented:
for each x in xmlDoc.documentElement.childNodes
    Exec(x.nodeName & " = """ & x.text & """")
next

This will create a variable called "to", but as it's a dynamic variable, you'll struggle accessing it, as variables aren't contained within any form of collection.

Maybe you should place them inside a Dictionary object, but this will only allow one "to" variable.

Set objVariables = Server.CreateObject("Scripting.Dictionary")

for each x in xmlDoc.documentElement.childNodes
    objVariables(x.nodeName) = x.text
next

And then to loop through the "variables", use this:

For Each x In objVariables.Keys
    Response.Write "Variable (" & x & ") = " & objVariables.Items(x) & "<br />"
Next
0
 
stevenbakerCommented:
What do you then need to do with these variables?
Depending on this I would suggest just keeping the XML file to access your variables.

If you do need them as ASP vars then I would have thought the dictionary object, or, if the only 1 "to" variable limitation is unrealistic you could create a 2 dimensional array.  However, I would stick to the XML file and work that way.

if you did want to go the array route:
<%

Set xmlDOC = Server.CreateObject("Microsoft.XMLDOM")
xmlDoc.async = False
'load xml file
xmlDOC.Load(Server.MapPath("note.xml"))


dim varArr()
redim varArr(xmlDoc.documentElement.childNodes.length,2)

for i=0 to xmlDoc.documentElement.childNodes.length

varArr(i,0)=xmlDoc.documentElement.childNodes.item(i).nodeName
varArr(i,0)=xmlDoc.documentElement.childNodes.item(i).childnodes(0).nodeValue

next
%>
0
 
fox_stattonAuthor Commented:
I tried one of the solution by AlfaNoMOre

and got

Microsoft VBScript runtime error '800a000d'

Type mismatch: 'Exec'

/sf27621/config/config.asp, line 9
0
 
GaryCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Accept Answer by AlfaNoMore

Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
GaryC123
EE Cleanup Volunteer
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now