Solved

Converting some XML to variables in ASP

Posted on 2004-10-06
7
286 Views
Last Modified: 2012-05-05
Hi, I would like to open a text file containing some XML, looping stuff  and putting it in variables. I did dis before in Coldfusion and normally don't work woth ASP. I was hoping for a 'working example' for the following scenario.
Thanks in advance! Floris.

ps. making question difficult. 250 points is ok when asking working example in case like this? tanx.


textfile.xml; xml format; 0..x time a thing struct.
<things>
 <thing>
  <id>1</id>
  <name>bla</name>
 </thing>
 <thing>
  <id>3</id>
  <name>bli</name>
 </thing>
<things>

asp file in pseudo code (I'm looking for the real stuff please!-)..)

<%
Dim mVar,myXMLVar, x

x = 1
myXMLVar = openXmlFile("textfile.xml")

While not myXMLVar.End do
 myVar = myXMLVar.thing
 response.write "thing" & x & ":<br>"
 response.write myVar.id & "<br>"
 response.write myVar.name & "<br>"
 x = x + 1
end while
%>

which should given the xml, then give the output:
thing1
1
bla
thing2
3
bli




0
Comment
Question by:florisb
7 Comments
 
LVL 20

Expert Comment

by:jitganguly
ID: 12238384
0
 
LVL 19

Expert Comment

by:peh803
ID: 12238446
Here's a good tutorial that will not only give you a working example, but explain why it's doing what it's doing.

http://stardeveloper.com:8080/articles/display.html?article=2000072801&page=1

Apologies to any that may have recently posted if this is a repost...

peh803
0
 
LVL 2

Author Comment

by:florisb
ID: 12238486
ah, what the.. ..got enough points and really need some fast help. bit more points.
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 6

Expert Comment

by:gete
ID: 12245288
Following your pseudo code:

<%
Dim oXML, oNode, x

Set oXML = Server.CreateObject("MSXML.DOMDocument")
oXML.async = False
oXML.load Server.MapPath("textfile.xml")

x = 1
For Each oNode In oXML.selectNodes("/things/thing")
    Response.Write "thing" & x & ":<br>"
    Response.Write oNode.getAttribute("id") & "<br>"
    Response.Write oNode.getAttribute("name") & "<br>"
    x = x + 1
Next
%>

I have only "compiled" this in my head. If you encounter any error, let us know.
0
 
LVL 2

Author Comment

by:florisb
ID: 12248268
hi gete, shouldn't oNode get a value in the loop?
0
 
LVL 2

Author Comment

by:florisb
ID: 12248304
<%
Dim oXML, oNode, x

Set oXML = Server.CreateObject("MSXML.DOMDocument")
oXML.async = False
oXML.load Server.MapPath("test.xml")

x = 1
For Each oNode In oXML.selectNodes("/bouwlocaties/bouwlocatie")
    Response.Write "bouwlocatie" & x & ":<br>"
    Response.Write oNode.getAttribute("id") & "<br>"
    Response.Write oNode.getAttribute("name") & "<br>"
    x = x + 1
Next
%>

with

<?xml version="1.0" encoding="iso-8859-1" ?>
<bouwlocaties>
 <bouwlocatie>
  <id>1001</id>
  <name>in de bushbush</name>
 </bouwlocatie>
 <bouwlocatie>
  <id>1002</id>
  <name>op het dak</name>
 </bouwlocatie>
</bouwlocaties>

gives me just

bouwlocatie1:


bouwlocatie2:

and no values...;-(

thanks!
0
 
LVL 6

Accepted Solution

by:
gete earned 400 total points
ID: 12254604
My mistake.. too much coding using attributes instead of child nodes in storing values.
This should work:

<%
Dim oXML, oNode, x

Set oXML = Server.CreateObject("MSXML.DOMDocument")
oXML.async = False
oXML.load Server.MapPath("test.xml")

x = 1
For Each oNode In oXML.selectNodes("/bouwlocaties/bouwlocatie")
    Response.Write "bouwlocatie" & x & ":<br>"
    Response.Write oNode.selectSingleNode("id").text & "<br>"
    Response.Write oNode.selectSingleNode("name").text & "<br>"
    x = x + 1
Next
%>
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:  The Exchange of information …
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

820 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