Using ASP to generate XML documents from SQL

C

cmt

Greetings everyone,

I'm building an XML document based on data from a bunch of SQL
queries. The queries call data from multiple tables...not just one.

It feels as if the method I am using is cumbersome and I am wondering
if there is a better method.

Currently I am just building the XML document like this:

Code:
SQL1  'SQL query 1

Set rs1 = conn.Execute(SQL1)

If Not (rs1.EOF = True And rs1.bof = True) Then
Set inode = xmldoc.createNode("element", "fielda, "")
onode.appendChild (inode)
Set child = xmldoc.createNode("element", "fieldb", "")
child.Text = rs1.fields(0)
inode.appendChild (child)
Set child = xmldoc.createNode("element", "fieldc", "")
child.Text = rs1.fields(1)
inode.appendChild (child)


SQL2  'SQL query 2

Set rs2 = conn.Execute(SQL2)

If Not (rs2.EOF = True And rs2.bof = True) Then
Set inode = xmldoc.createNode("element", "fielda", "")
onode.appendChild (inode)
Set child = xmldoc.createNode("element", "fieldb", "")
child.Text = rs2.fields(0)
inode.appendChild (child)
Set child = xmldoc.createNode("element", "fieldc", "")
child.Text = rs2.fields(1)
inode.appendChild (child)

SQL3  'SQL query 3

Set rs3 = conn.Execute(SQL3)

If Not (rs3.EOF = True And rs3.bof = True) Then
Set inode = xmldoc.createNode("element", "fielda", "")
onode.appendChild (inode)
Set child = xmldoc.createNode("element", "fieldb", "")
child.Text = rs3.fields(0)
inode.appendChild (child)
Set child = xmldoc.createNode("element", "fieldc", "")
child.Text = rs3.fields(1)
inode.appendChild (child)

This is just an example version. The actual code is about 1000 lines
long.

Does ASP have a better way of handling the creation of XML?

Thanks!
 
B

Bob Barrows [MVP]

cmt said:
Greetings everyone,

Does ASP have a better way of handling the creation of XML?
Given that ASP is not a language, the answer to your literal question
has to be "no". Unfortunately, the answer to your implied question (does
vbscript hava a better way ... ) is also no.

Since it appears you are using SQL Server (if that's what you mean by
the "SQL" in your subject line) you might want to investigate the FOR
XML clause in SQL 2000 and SQL 2005 Without knowing the version of SQL
Server you are using, i can't get specific, so all I can suggest is that
you look it up in BOL.
 
A

Anthony Jones

cmt said:
Greetings everyone,

I'm building an XML document based on data from a bunch of SQL
queries. The queries call data from multiple tables...not just one.

It feels as if the method I am using is cumbersome and I am wondering
if there is a better method.

Currently I am just building the XML document like this:

Code:
SQL1  'SQL query 1

Set rs1 = conn.Execute(SQL1)

If Not (rs1.EOF = True And rs1.bof = True) Then
Set inode = xmldoc.createNode("element", "fielda, "")
onode.appendChild (inode)
Set child = xmldoc.createNode("element", "fieldb", "")
child.Text = rs1.fields(0)
inode.appendChild (child)
Set child = xmldoc.createNode("element", "fieldc", "")
child.Text = rs1.fields(1)
inode.appendChild (child)


SQL2  'SQL query 2

Set rs2 = conn.Execute(SQL2)

If Not (rs2.EOF = True And rs2.bof = True) Then
Set inode = xmldoc.createNode("element", "fielda", "")
onode.appendChild (inode)
Set child = xmldoc.createNode("element", "fieldb", "")
child.Text = rs2.fields(0)
inode.appendChild (child)
Set child = xmldoc.createNode("element", "fieldc", "")
child.Text = rs2.fields(1)
inode.appendChild (child)

SQL3  'SQL query 3

Set rs3 = conn.Execute(SQL3)

If Not (rs3.EOF = True And rs3.bof = True) Then
Set inode = xmldoc.createNode("element", "fielda", "")
onode.appendChild (inode)
Set child = xmldoc.createNode("element", "fieldb", "")
child.Text = rs3.fields(0)
inode.appendChild (child)
Set child = xmldoc.createNode("element", "fieldc", "")
child.Text = rs3.fields(1)
inode.appendChild (child)

This is just an example version. The actual code is about 1000 lines
long.

Does ASP have a better way of handling the creation of XML?

It would really help if you indicated what DB you are using. Also have you
oversimplified your code or do you really not need to loop through the
recordsets.

SQL Server has a With XML modifier that you can use to generate XML from SQL
directly. Whilst its syntax can be a little cumbersome it can generate
hiearchical XML that you seem to want to build.

If your not using SQL Server and the DB you are using isn't able to generate
XML then you'll need to do it the hard way.

First you need to learn to use functions like this one:-

Function AddElem(roParent, rsName, rvntValue)
Set AddElem = roParent.ownerDocument.createElement(rsName)
roParent.appendChild AddElem
If Not IsNull(rvntValue) Then AddElem.Text = rvntValue
End Function

Then code can look like this:-

Set inode = AddElem(onode, "fielda", Null)
AddElem inode, "fieldb", rs3.fields(0).value
AddElem inode, "fieldc", rs3.fields(1).value

If your code is mainly as your example then you'll eliminate 66% of you code
with just that function.

Is it possible to include the results of the some of the recordsets in a
single query using JOINS? This doesn't necessarily prevent you from
creating the heiarchy you need.
 
T

TOUDIdel

Uzytkownik "cmt said:
Does ASP have a better way of handling the creation of XML?

Better not but in less rows' count :)

while(!r.EOF){
lml.appendChild(lxml.createElement(raw?raw:'I'))
for(var k=0;k<r.Fields.Count;k++){var fn=new
String(r.Fields(k).Name),fv=new
String(r.Fields(k).Value);llml.setAttribute(fn,fv);};
r.MoveNext();
};
 
B

Bob Barrows [MVP]

cmt said:
Thanks everyone!

I am actually using SQL Server 2000.

OK, go into BOL and look up For XML clause. On my machine, this link
gets me to the "guidelines" article (look at the "See Also links as
well):

mk:mad:MSITStore:C:\Program%20Files\Microsoft%20SQL%20Server\80\Tools\Books
\xmlsql.chm::/ac_openxml_0alh.htm
 

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,968
Messages
2,570,150
Members
46,697
Latest member
AugustNabo

Latest Threads

Top