Groups | Blog | Home
all groups > inetserver asp general > february 2007 >

inetserver asp general : Displaying XML in ASP


lejason NO[at]SPAM gmail.com
2/27/2007 1:32:20 PM
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?
%>
lejason NO[at]SPAM gmail.com
2/27/2007 4:31:12 PM
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?
Anthony Jones
2/27/2007 10:32:52 PM

[quoted text, click to view]

Yes you are.

[quoted text, click to view]

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.

[quoted text, click to view]

Parentheses should not be used here use:-

xdoc.load "c:\test.xml"

[quoted text, click to view]

Parentheses in response.writes not needed.

[quoted text, click to view]

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

Set NodeList = xdoc.SelectNodes("/root/person")

[quoted text, click to view]

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.

[quoted text, click to view]

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.



Evertjan.
2/28/2007 12:00:00 AM
wrote on 28 feb 2007 in microsoft.public.inetserver.asp.general:

[quoted text, click to view]

[please always quote on usenet]

--
Evertjan.
The Netherlands.
Anthony Jones
2/28/2007 12:00:00 AM

[quoted text, click to view]

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

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


[quoted text, click to view]

Aaron Bertrand [SQL Server MVP]
2/28/2007 3:53:31 PM
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.




[quoted text, click to view]

Mark J. McGinty
2/28/2007 6:55:01 PM

[quoted text, click to view]

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


[quoted text, click to view]

AddThis Social Bookmark Button