Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2003-03-26
5
Medium Priority
?
200 Views
Last Modified: 2008-03-17
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
Comment
Question by:fox_statton
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 

Expert Comment

by:stezis
ID: 8209175
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
 
LVL 9

Accepted Solution

by:
AlfaNoMore earned 200 total points
ID: 8209233
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
 
LVL 2

Expert Comment

by:stevenbaker
ID: 8209421
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
 

Author Comment

by:fox_statton
ID: 8210229
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
 
LVL 58

Expert Comment

by:Gary
ID: 9188003
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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question