[quoted text, click to view] <lejason@gmail.com> wrote in message
news:1172611939.994146.145250@k78g2000cwa.googlegroups.com...
> 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.
[quoted text, click to view] >
> ' 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.
[quoted text, click to view] > xdoc.async=false
> xdoc.load("c:\test.xml")
Parentheses should not be used here use:-
xdoc.load "c:\test.xml"
[quoted text, click to view] >
> 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.
[quoted text, click to view] >
>
> 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")
[quoted text, click to view] > 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.
[quoted text, click to view] > Next
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.