ASP, XML stuff...

D

Dirty Davey

Hi all,

I'm trying o get my head around XML and using ASP to parse documents.

I've managed to kncok together the code below to send a request to an
external server and then pop the response into a variable and format it
a bit.

What I'm trying to do is insert the data into a nice HTML table, maybe
using the node names as column headings. Has anyone got any suggestions
on how I can do this? and also, is this the best methid of performing
this task? I need it to be xbrowser compatible so I figured server side
would be the best option.

Big thanks in advance,

T

<head>
<%

Sub TraverseTree(obj)



For i = 0 to (obj.childNodes.length -1)
if obj.childNodes.item(i).nodeName <> "#text" then
set node = obj.childNodes.item(i)
TraverseTree(node)
else
response.write("<tr>")
Response.Write("<td><font color='black'><b>" &
obj.childNodes.item(i).parentNode.nodeName & "</b>, </font></td>")
Response.Write("<td><font color='red'>" & obj.childNodes.item(i).data &
"<br></font></td>")
'execute(obj.childNodes.item(i).parentNode.nodeName =
obj.childNodes.item(i).data)
end if
response.write("</tr>")
next


end Sub
%>

</head>

<body>
<%
' Declare the variables
dim objHTTP, myData, myXML

' Assign my XML request fragment
myXML = myXML & "<GetInfo>" & vbCrLf
myXML = myXML & " <User>" & vbCrLf
myXML = myXML & " <ID>1</ID>" & vbCrLf
myXML = myXML & " </User>" & vbCrLf
myXML = myXML & " <Period>" & vbCrLf
myXML = myXML & " <StartDate>01/01/2006</StartDate>" & vbCrLf
myXML = myXML & " <EndDate>01/06/2006</EndDate>" & vbCrLf
myXML = myXML & " </Period>" & vbCrLf
myXML = myXML & " <Authentication>" & vbCrLf
myXML = myXML & " <Username>user</Username>" & vbCrLf
myXML = myXML & " <Password>pass</Password>" & vbCrLf
myXML = myXML & " </Authentication>" & vbCrLf
myXML = myXML & "</GetInfo>" & vbCrLf

'response.write myXML & "<br />"

' Create the HTTP Object
set objHTTP = server.CreateObject("MSXML2.ServerXMLHTTP")

' Prepare the object
objHTTP.open "POST","https://www.myexternalurl.com/",false

' Set the content-type for the HTTP Request
objHTTP.setRequestHeader "Content-Type",
"application/x-www-form-urlencoded"

' Set the form variable and encode the XML fragment
myData = "requestxml=" & Server.URLEncode(myXML)

' Perform the request
objHTTP.send myData

'response.write objHTTP.responseText

dim objList
Set objList = CreateObject("Microsoft.XMLDOM")
objlist.async = true
objList.validateOnParse = true
objList.loadxml(objHTTP.responseText)

response.write("<table>")
TraverseTree(objList.documentElement)
response.write("</table>")

set objHTTP = nothing
%>
 
B

Bob Barrows [MVP]

Dirty said:
Hi all,

I'm trying o get my head around XML and using ASP to parse documents.

I've managed to kncok together the code below to send a request to an
external server and then pop the response into a variable and format
it a bit.

What I'm trying to do is insert the data into a nice HTML table, maybe
using the node names as column headings. Has anyone got any
suggestions on how I can do this?

What's the problem you are encountering? i'm not going to plow through all
that code without some idea of what I'm looking for.
 
D

Dirty Davey

Sure, it's this bit:

For i = 0 to (obj.childNodes.length -1)
if obj.childNodes.item(i).nodeName <> "#text" then
set node = obj.childNodes.item(i)
TraverseTree(node)
else
response.write("<tr>")
Response.Write("<td><font color='black'><b>" &
obj.childNodes.item(i).parentNode.nodeName & "</b>, </font></td>")
Response.Write("<td><font color='red'>" & obj.childNodes.item(i).data &

"<br></font></td>")
'execute(obj.childNodes.item(i).parentNode.nodeName =
obj.childNodes.item(i).data)
end if
response.write("</tr>")
next

How can I turn what it outputs into a table with the node names as
column headings?

Thanks for the response,
 
B

Bob Barrows [MVP]

Without running this code, I can't really tell what it is outputting
now. It seems to be writing some td elelments to response now. I see you
using the nodename property so you know about that ... I just don't
understand what your problem is. Simply loop through the nodes, writing
the node names into the first row of the table. Then loop through the
childnodes, writing the data into the subsequent rows of the table.
Without knowing what the xml looks like, this is the best I can do.
 
B

Bob Barrows [MVP]

Dirty said:
Sure, it's this bit:

Here is a short demo I whipped up that may help:

<%@ Language=VBScript %>
<%
dim xml, xmldoc, node, colnode
xml="<rows><row><col1>a</col1><col2>b</col2>" & _
"</row><row><col1>c</col1><col2>d</col2></row></rows>"
set xmldoc=createobject("msxml2.domdocument")
xmldoc.loadXML xml
set node=xmldoc.selectSingleNode("/rows/row")
Response.Write "<table border=""1"" style=""" & _
"border-collapse:collapse""><tr>"
for each colnode in node.childnodes
Response.Write "<th>" & colnode.nodename & "</th>"
next
Response.Write "</tr>"
for each node in xmldoc.documentElement.childnodes
Response.Write "<tr>"
for each colnode in node.childnodes
Response.Write "<td>" & colnode.text & "</td>"
next
Response.Write "</tr>"
next
Response.Write "</table>"

%>

There are those that maintain that using xsl transformations is the way
to go for this. If you'd like help with that, try an xml newsgroup.
 
A

Anthony Jones

Dirty Davey said:
Hi all,

I'm trying o get my head around XML and using ASP to parse documents.

I've managed to kncok together the code below to send a request to an
external server and then pop the response into a variable and format it
a bit.

What I'm trying to do is insert the data into a nice HTML table, maybe
using the node names as column headings. Has anyone got any suggestions
on how I can do this? and also, is this the best methid of performing
this task? I need it to be xbrowser compatible so I figured server side
would be the best option.

Big thanks in advance,

A few things.

Why are you posting the XML as Form field? Why not just post it directly:-

' Prepare the object
objHTTP.open "POST","https://www.myexternalurl.com/",false

' Set the content-type for the HTTP Request
objHTTP.setRequestHeader "Content-Type", "text/xml"

' Perform the request
objHTTP.send myXML 'Note no nasty URLEncoding

The receiving page can then load xml into a DOM directly from the request
object:-

Dim oDOM
Set oDOM = Server.CreateObject("MSXML2.DOMDocument.3.0")
oDOM.load Request

Also make sure the receiving page does this:-

Response.ContentType = "text/xml"

Now back in your code you do not need to load a DOM with the responseText
property. The responseXML property will return a parsed DOM to you. Just
be sure to test status = 200 to check that the response is ok.

As to transforming the XML in to HTML output. An alternative to
ASP/VBScript is XSLT which IMO is more suited to this task.

Taking Bob's example of a possible XML result the XSLT below will generate a
tabular output.

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:eek:utput method="html" encoding="Windows-1252" />

<xsl:template match="rows">
<html>
<body>
<table>
<thead>
<tr>
<xsl:for-each select="row[1]/*">
<th><xsl:value-of select="local-name()" /></th>
</xsl:for-each>
</tr>
</thead>
<tbody>
<xsl:for-each select="row">
<tr>
<xsl:for-each select="*">
<td><xsl:value-of select="." /></td>
</xsl:for-each>
</tr>
</xsl:for-each>
</tbody>
</table>
</body>
</html>
</xsl:template>

</xsl:stylesheet>

Put this xsl into a file called myTransform.xsl

Here is a simple example of using this transform in your code:-

Dim oXSL
Set oXSL = Server.CreateObject("MSXML2.DOMDocument.3.0")
oXSL.async = false
oXSL.load Server.MapPath("myTransform.xsl")

objHTTP.responseXML.transformNodeToObject oXSL, Response

http://www.w3schools.com/xsl/default.asp

Anthony.
 
B

Bob Barrows [MVP]

Anthony said:
As to transforming the XML in to HTML output. An alternative to
ASP/VBScript is XSLT which IMO is more suited to this task.
Thanks, Anthony, I was hoping someone would post an xsl transformation for
this.
 

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

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top