Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multiple datasets in XML 1

Status
Not open for further replies.

Corneliu

Technical User
Sep 16, 2002
141
US
I have a basic ASP page that reads a XML file. The files consists of same data fields, but more than once.
EX:
Program INDI
Title Baster
SubTitle Fun Time
Desc blah blah blah

Program MONSTER
Title Medic
Subtitle TimeOut
Desc blash blah blah

Basically I want to take all the data and put them in separate forms, each one with a submit button. Once i submit it, the data will be entered into an access database. I am now able to read the XML file and write the data to the DB, but only 1 of the entries. I will probably need to create a loop, but don't know how. Anyone can help me here PLEASE?
THANK YOU

Code so far:
<% @Language = "VBScript" %>
<% Response.buffer = true %>

<%

Dim xml
Set xml = Server.CreateObject("Microsoft.XMLDOM")
xml.async = False
xml.load (Server.MapPath("Programs.xml"))

Dim program, title, subtitle, desc
program = xml.documentElement.childNodes(0).text
title = xml.documentElement.childNodes(1).text
subtitle = xml.documentElement.childNodes(2).text
desc = xml.documentElement.childNodes(3).text

Set xml = Nothing
%>
<form action="ProgramSubmit.asp" method="post">

<input name="title" value="<%= title %>" type="text">
<input name="program" value="<%= program %>" type="text">
<input name="subtitle" value="<%= subtitle %>" type="text">
<textarea name="desc" width="400"><%= desc %></textarea>
<input type="submit" name="Submit" value="Submit">
</form>
 
Hmm...an example of the XML would be helpful. My first instinct would be to assume that it is structured something like:
Code:
<programs>
   <program>
      <name>INDI</name>
      <title>Baster</title>
      <subtitle>Fun Time</subtitle>
      <desc>blah blah blah</desc>
   </program>
   <program> ...etc

</programs>

But your code wouldn't be working properly to open one of those items. According to your code it looks something like:
Code:
<programs>
   <name>INDI</name>
   <title>Baster</title>
   <subtitle>Fun Time</subtitle>
   <desc>blah blah blah</desc>
   ...etc
</programs>

The problem with the second structure is that all the fields are on the same level, so adding more tags:
Code:
<programs>
   <name>INDI</name>
   <title>Baster</title>
   <subtitle>Fun Time</subtitle>
   <desc>blah blah blah</desc>
   <name>MONSTER</name>
   <title>Medic</title>
   <subtitle>TimeOut</subtitle>
   <desc>blash blah blah</desc>
</programs>
This makes it difficult to tell which name/title/etc belong with the others.

What your going to want to do is set a variable to the documentElement. Then you can call the .SelectNodes(tagname) method which will return a node collection of everything that matchs the tagname you gave it.
the reason I'm hoping it looks like the first structure is because you could .SelectNodes("program") then loop through the collection of programs, referencing the inner nodes very similarly to how your doing it in your sample code.

-T

[sub]01000111 01101111 01110100 00100000 01000011 01101111 01100110 01100110 01100101 01100101 00111111[/sub]
The never-completed website:
 
Thankx Tarwn, I thought it would not be that easy. I attached the XML format below. Hopefully is not hard to come up with a solution, I only hope. THANK YOU...

XML Format:

<?xml version="1.0" ?>
- <tvschedule nextID="6">
- <recording start-padding="0" stop-padding="0" canceled="no" season-ticket-id="0" id="1">
- <programme start="20040512233000 GMT" stop="20040513000000 GMT" channel="11 WFLX" clumpidx="">
<title>Everybody Loves Raymond</title>
<sub-title>Just a Formality</sub-title>
<desc>Robert proposes to Amy; Ray meets Amy's ultraconservative parents.</desc>
</programme>
</recording>
- <recording start-padding="0" stop-padding="0" canceled="no" season-ticket-id="0" id="2">
- <programme start="20040513030000 GMT" stop="20040513033000 GMT" channel="60 TOON" clumpidx="">
<title>Futurama</title>
<sub-title>Anthology of Interest 1</sub-title>
<desc>The professor invents a ``what if'' machine; guest voice Al Gore.</desc>
- <rating system="VCHIP">
<value>PG</value>
</rating>
</programme>
</recording>
- <recording start-padding="0" stop-padding="0" canceled="no" season-ticket-id="0" id="3">
- <programme start="20040513020000 GMT" stop="20040513030000 GMT" channel="66 FX" clumpidx="">
<title>Nip/Tuck</title>
<sub-title>Cara Fitzgerald</sub-title>
<desc>Christian takes matters into his own hands when he learns a patient lied to him.</desc>
<category>Drama</category>
</programme>
</recording>
- <recording start-padding="0" stop-padding="0" canceled="no" season-ticket-id="0" id="4">
- <programme start="20040514060000 GMT" stop="20040514063000 GMT" channel="60 TOON" clumpidx="">
<title>Futurama</title>
<sub-title>War Is the H-Word</sub-title>
<desc>Fry and Bender join the military for soldiers' discounts.</desc>
- <rating system="VCHIP">
<value>PG</value>
</rating>
</programme>
</recording>
- <recording start-padding="0" stop-padding="0" canceled="no" season-ticket-id="0" id="5">
- <programme start="20040514023000 GMT" stop="20040514030000 GMT" channel="68 SCIFI" clumpidx="">
<title>Tripping the Rift</title>
<sub-title>Aliens, Guns & a Monkey</sub-title>
<desc>The crew must repair their ship after crash landing on a tumultuous planet.</desc>
<category>SciFi</category>
- <rating system="VCHIP">
<value>MA</value>
</rating>
</programme>
</recording>
</tvschedule>

Can it be done? Any help would appreciate it very much. Still learning XML myself.

THANK YOU VERY MUCH...
 
Tarwn, after I been searching around the forums, found a code that might work but have 2 problems.
1: Not all the words appear in the text boxes.
Ex: Everybody loves Raymond
With the way I did it, the box only shows "Everybody", nothing else. It appears that if there is a space in between the words, it wont show.
2: Not all the records show up, only 3. I have a test page with 6 records (6 different shows), but only 3 show up.
Don't know if my code is wrong or if I am doing it wrong. Can you or anyone help with this one please?
THANK YOU

<%

Dim xml
Set xml = Server.CreateObject("Microsoft.XMLDOM")
xml.async = False
xml.load (Server.MapPath("schedule.xml"))

For intCounter = 0 to (xml.documentElement.childNodes.length - 1)
Heading = xml.getElementsByTagName("title").item(intCounter).text
SubHeading = xml.getElementsByTagName("sub-title").item(intCounter).text
descript = xml.getElementsByTagName("desc").item(intCounter).text
Response.Write "<form action=""postmovies.asp"" method=""post"">"
Response.Write "Title"
Response.Write "<input name=""title"" value=" & Heading & " type=""text"">"
Response.Write "Episode Name"
Response.Write "<input name=""subtitle"" value=" & SubHeading & " type=""text"">"
Response.Write "<textarea name=""desc"" cols=""100"" value=" & descript & " rows=""5"" type=""text"">"
Response.Write descript
Response.Write "</textarea>"
Response.Write "<input type=""submit"" name=""Submit"" value=""Submit"">"
Response.Write "</form>"
intCounter = intCounter + 1
Response.Write "<br>"
Next

Set xml = Nothing
%>

The XML code is the same as before.
THANK YOU VERY MUCH...
 
The break on spaces thing is a pretty common error, all that means is that you need quotes around the value, here is one of your lines with the additional quotes:
Code:
Response.Write "<input name=""title"" value=[highlight]""[/highlight]" & Heading & "[highlight]""[/highlight] type=""text"">"

The double-double quotes act as an escape character for double quotes, so the end results will look something like:
<input name="title" value="some value" type="text">
That way it won't break off on spaces.

I'm not sure why your second problem is occuring, but I would do thins slightly differant to make thm more efficient. Right now your looping thorugh all the parent nodes and inside each loop doing searches for tagname collecitons. It would be better if you did the searches once, or only searchd one time period. Here is what I was thinking in the last post of mine:
Code:
<%

Dim xml
    Set xml = Server.CreateObject("Microsoft.XMLDOM")
    xml.async = False
    xml.load (Server.MapPath("schedule.xml"))

If xml.ParseError.ErrorCode <> 0 Then
	Response.Write "<h1>Parse Error Occurred: </h1><br>" & _
					"Line: " & xml.ParseError.Line & "<br>" & _
					"LinePos: " & xml.ParseError.LinePos & "<br>" & _
					"Source Text: " & xml.ParseError.SrcText & "<br>" & _
					"Parse Error: " & xml.ParseError.Reason & "<br>"
	Response.End
End If

Dim progs, prog
Set progs = xml.DocumentElement.getElementsByTagname("programme")
Response.Write "Progs: " & progs.length & "<br>"

For Each prog in progs
    Heading = prog.childNodes(0).text
    SubHeading = prog.childNodes(1).text
    descript = prog.childNodes(2).text
    Response.Write "<form action=""postmovies.asp"" method=""post"">"
    Response.Write "Title"
    Response.Write "<input name=""title"" value=""" & Heading & """ type=""text"">"
    Response.Write "Episode Name"
    Response.Write "<input name=""subtitle"" value=""" & SubHeading & """ type=""text"">"
    Response.Write "<textarea name=""desc"" cols=""100"" rows=""5"" type=""text"">"
    Response.Write descript
    Response.Write "</textarea>"
    Response.Write "<input type=""submit"" name=""Submit"" value=""Submit"">"
    Response.Write "</form>"
    Response.Write "<br>"
Next

Set xml = Nothing
Set progs = Nothing
Set prog = Nothing
%>

This way only one search ever happens, should make things more efficient. The only danger you may run into is if the order of the first three tags (title, subtitle, description) ever changes.

-T

[sub]01000111 01101111 01110100 00100000 01000011 01101111 01100110 01100110 01100101 01100101 00111111[/sub]
The never-completed website:
 
THANK YOU THANK YOU THANK YOU VERY MUCH.

WORKS LIKE A CHARM.

Again, you are the best Tarwn.

NOTE: I like your site, very nice.


THANK YOU AGAIN SIR...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top