Displaying XML in ASP

L

lejason

I am having trouble with a really simple problem! haha. How do you
display that data from on xml file. Here is my xml file called
"test.xml"

<?xml version="1.0" encoding="iso-8859-1"?>
<test>
<person>
<name>Jason</name>
<color>blue</color>
<number>16</number>
</person>
<person>
<name>Josh</name>
<color>red</color>
<number>10</number>
</person>
<person>
<name>Justin</name>
<color>blue</color>
<number>7</number>
</person>
</test>


Now, what I want to do is to have it display all the elements per
<person>. For example, I want an output to say be something to the
extent of....

Jason
blue
16

Josh
red
10

Justin
blue
7

So here is the ASP I have set up...but it doesnt work. I am having
problems with the syntax I think?


' Sets up the DOM

Dim xdoc
Set xdoc=Server.CreateObject("Microsoft.XMLDOM")
xdoc.async=false
xdoc.load("c:\test.xml")

if xdoc.parseError.errorcode<>0 then
response.write("there was obviously an error")
else
response.write("Things worked <br><br>")
end if


Set Root = xdoc.documentElement
Set NodeList = Root.getElementsByTagName("person")
For Each Elem In NodeList
response.write(Elem.firstChild.firstchild.nodeValue)
Next

============================

that code wont error, but it only shows the firstchild. So I tired
Elem.firstChild.childNodes(1).nodeValue right after it and it broke
"Object requried".

WTf?

help?
%>
 
A

Anthony Jones

I am having trouble with a really simple problem! haha. How do you
display that data from on xml file. Here is my xml file called
"test.xml"

<?xml version="1.0" encoding="iso-8859-1"?>
<test>
<person>
<name>Jason</name>
<color>blue</color>
<number>16</number>
</person>
<person>
<name>Josh</name>
<color>red</color>
<number>10</number>
</person>
<person>
<name>Justin</name>
<color>blue</color>
<number>7</number>
</person>
</test>


Now, what I want to do is to have it display all the elements per
<person>. For example, I want an output to say be something to the
extent of....

Jason
blue
16

Josh
red
10

Justin
blue
7

So here is the ASP I have set up...but it doesnt work. I am having
problems with the syntax I think?

Yes you are.
' Sets up the DOM

Dim xdoc
Set xdoc=Server.CreateObject("Microsoft.XMLDOM")

Use MSXML2.DOMDocument.3.0, the ProgID above it ambiguous it's likely to
return a DOM document version 3 but it may not. Also this version specific
ProgID tightens up the XML syntax parsing a little.
xdoc.async=false
xdoc.load("c:\test.xml")

Parentheses should not be used here use:-

xdoc.load "c:\test.xml"
if xdoc.parseError.errorcode<>0 then
response.write("there was obviously an error")
else
response.write("Things worked <br><br>")
end if

Parentheses in response.writes not needed.
Set Root = xdoc.documentElement
Set NodeList = Root.getElementsByTagName("person")

The following IMO would be better although in this case the result is the
same:-

Set NodeList = xdoc.SelectNodes("/root/person")
For Each Elem In NodeList
response.write(Elem.firstChild.firstchild.nodeValue)

Each Elem will be a person node. The firstChild of a person element node is
a name element node. The firstChild of a name element node is a text node.
Hence the above code writes out the content of the name node of each person
node, other nodes in the person is ignored.

Ditching the NodeList variable we can get to this:-

For Each elemPerson in xdoc.SelectNodes("/root/person")
For Each elem in elemPerson.SelectNodes("*")
Response.Write elem.tagName & ": " elem.Text & "<br />"
Next
Next

Note the Text property of an element returns all the inner text of an
element. This saves you having to access the internal text node.
 
L

lejason

First off - THANKS :)

However...while it didnt error, the page produced nothing. So, using
the said xml file and now this code :

<%
' Sets up the DOM

Dim xdoc
Set xdoc=Server.CreateObject("Microsoft.XMLDOM")
xdoc.async=false
xdoc.load("c:\test.xml")

if xdoc.parseError.errorcode<>0 then
response.write "there was obviously an error"
else
response.write "Things worked <br><br>"
end if

For Each elemPerson in xdoc.SelectNodes("/root/person")
For Each elem in elemPerson.SelectNodes("*")
Response.Write elem.tagName & ": " & elem.Text & "<br />"
Next
Next
%>

========================================

All I get is a blank page that says "Things worked" Also, I should
mention that im not using ASP.NET...just plain old-school ASP. So, im
not sure if thats why its not working?

so...any ideas?
 
A

Anthony Jones

First off - THANKS :)

However...while it didnt error, the page produced nothing. So, using
the said xml file and now this code :

<%
' Sets up the DOM

Dim xdoc
Set xdoc=Server.CreateObject("Microsoft.XMLDOM")
xdoc.async=false
xdoc.load("c:\test.xml")

if xdoc.parseError.errorcode<>0 then
response.write "there was obviously an error"
else
response.write "Things worked <br><br>"
end if

For Each elemPerson in xdoc.SelectNodes("/root/person")

Oops didn't read your XML properly the root node is called 'test' in you
xml:-

For Each elemPerson in xdoc.SelectNodes("/test/person")
 
A

Aaron Bertrand [SQL Server MVP]

Ah, it's fun to quickly realize why I stay out of this group for months at a
time. Too much effort spent on correcting etiquette without adding any
substance to the conversation.



First off - THANKS :)

[please always quote on usenet]
 
M

Mark J. McGinty

Aaron Bertrand said:
Ah, it's fun to quickly realize why I stay out of this group for months at
a time. Too much effort spent on correcting etiquette without adding any
substance to the conversation.

Indeed, but 99.99% comes from a single source, and thus it is relatively
easily ignored (as well it should be)... once you get past the irritation
factor.


-Mark

First off - THANKS :)

[please always quote on usenet]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Search a string inside an xml node 0
XML and ASP 1
XML + ASP 7
ASP, XML stuff... 6
receive xml from https post in classic ASP 2
ASP with XML 0
Using ASP to loop thru XML file and generate HTML 4
Handle xml in ASP 3

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top