实战ASP(8):使用ASP、VB和XML建立运行于互联网上的应用程序(下)
2024-09-05 20:55:41
供稿:网友
使用asp、vb和xml建立运行于互联网上的应用程序(2)
在实际的编程过程中,你们应当使用一些方法使应用程序更加有高效性。你可以把asp中的关于取得数据的代码端搬到一个com应用程序中去然后创建一个xslt变换来显示返回的数据。好,我不多说了,现在你所要做的就是试一试吧!
option explicit
private rcommands as recordset
private rcustomers as recordset
private rcust as recordset
private scustlistcommand as string
private const dataurl = "http://localhost/xhttprequest/getdata.asp"
private arrcustomerids() as string
private enum actionenum
view_history = 0
view_recent_product = 1
end enum
private sub dgcustomers_click()
dim customerid as string
customerid = rcustomers("customerid").value
if customerid <> "" then
if optaction(view_history).value then
call getcustomerdetail(customerid)
else
call getrecentproduct(customerid)
end if
end if
end sub
private sub form_load()
call initialize
call getcustomerlist
end sub
sub initialize()
' 从数据库返回命令名和相应的值
dim sxml as string
dim vret as variant
dim f as field
sxml = "<?xml version=""1.0""?>"
sxml = sxml & "<command><commandtext>initialize</commandtext>"
sxml = sxml & "<returnsdata>true</returnsdata>"
sxml = sxml & "</command>"
set rcommands = getrecordset(sxml)
do while not rcommands.eof
for each f in rcommands.fields
debug.print f.name & "=" & f.value
next
rcommands.movenext
loop
end sub
function getcommandxml(command_name as string) as string
rcommands.movefirst
rcommands.find "command_name='" & command_name & "'", , adsearchforward, 1
if rcommands.eof then
msgbox "cannot find any command associated with the name '" & command_name & "'."
exit function
else
getcommandxml = rcommands("command_xml")
end if
end function
sub getrecentproduct(customerid as string)
dim sxml as string
dim xml as domdocument
dim n as ixmldomnode
dim productname as string
sxml = getcommandxml("recentpurchasebycustomerid")
set xml = new domdocument
xml.loadxml sxml
set n = xml.selectsinglenode("command/param[name='customerid']/value")
n.text = customerid
set xml = executespwithreturn(xml.xml)
productname = xml.selectsinglenode("values/productname").text
' 显示text域
txtresult.text = ""
me.txtresult.visible = true
dgresult.visible = false
' 显示product名
txtresult.text = "最近的产品是: " & productname
end sub
sub getcustomerlist()
dim sxml as string
dim i as integer
dim s as string
sxml = getcommandxml("getcustomerlist")
set rcustomers = getrecordset(sxml)
set dgcustomers.datasource = rcustomers
end sub
sub getcustomerdetail(customerid as string)
' 找出列表中相关联的id号
dim sxml as string
dim r as recordset
dim f as field
dim s as string
dim n as ixmldomnode
dim xml as domdocument
sxml = getcommandxml("custorderhist")
set xml = new domdocument
xml.loadxml sxml
set n = xml.selectsinglenode("command/param[name='customerid']/value")
n.text = customerid
set r = getrecordset(xml.xml)
' 隐藏 text , 因为它是一个记录集
txtresult.visible = false
dgresult.visible = true
set dgresult.datasource = r
end sub
function getrecordset(sxml as string) as recordset
dim r as recordset
dim xml as domdocument
set xml = getdata(sxml)
debug.print typename(xml)
on error resume next
set r = new recordset
r.open xml
if err.number <> 0 then
msgbox err.description
exit function
else
set getrecordset = r
end if
end function
function executespwithreturn(sxml as string) as domdocument
dim d as new dictionary
dim xml as domdocument
dim nodes as ixmldomnodelist
dim n as ixmldomnode
set xml = getdata(sxml)
if xml.documentelement.nodename = "values" then
set executespwithreturn = xml
else
'发生错误
set n = xml.selectsinglenode("response/data")
if not n is nothing then
msgbox n.text
exit function
else
msgbox xml.xml
exit function
end if
end if
end function
function getdata(sxml as string) as domdocument
dim xhttp as new xmlhttp30
xhttp.open "post", dataurl, false
xhttp.send sxml
debug.print xhttp.responsetext
set getdata = xhttp.responsexml
end function
private sub optaction_click(index as integer)
call dgcustomers_click
end sub
代码二、getdata.asp
<%@ language=vbscript %>
<% option explicit %>
<%
sub responseerror(sdescription)
response.write "<res |||中国最大的web开发资源网站及技术社区,
ponse><data>error: " & sdescription & "</data></response>"
response.end
end sub
response.contenttype="text/xml"
dim xml
dim commandtext
dim returnsdata
dim returnsvalues
dim recordsaffected
dim param
dim paramname
dim paramtype
dim paramdirection
dim paramsize
dim paramvalue
dim n
dim nodename
dim nodes
dim conn
dim sxml
dim r
dim cm
' 创建domdocument对象
set xml = server.createobject("msxml2.domdocument")
xml.async = false
' 装载post数据
xml.load request
if xml.parseerror.errorcode <> 0 then
call responseerror("不能装载 xml信息。 描述: " & xml.parseerror.reason & "<br>行数: " & xml.parseerror.line)
end if
' 客户端必须发送一个commandtext元素
set n = xml.selectsinglenode("command/commandtext")
if n is nothing then
call responseerror("missing <commandtext> parameter.")
else
commandtext = n.text
end if
' 客户端必须发送一个returnsdata或者returnsvalue元素
set n = xml.selectsinglenode("command/returnsdata")
if n is nothing then
set n = xml.selectsinglenode("command/returnsvalues")
if n is nothing then
call responseerror("missing <returnsdata> or <returnsvalues> parameter.")
else
returnsvalues = (lcase(n.text)="true")
end if
else
returnsdata=(lcase(n.text)="true")
end if
set cm = server.createobject("adodb.command")
cm.commandtext = commandtext
if instr(1, commandtext, " ", vbbinarycompare) > 0 then
cm.commandtype=adcmdtext
else
cm.commandtype = adcmdstoredproc
end if
' 创建参数
set nodes = xml.selectnodes("command/param")
if nodes is nothing then' 如果没有参数
elseif nodes.length = 0 then
' 如果没有参数
else
for each param in nodes
' response.write server.htmlencode(param.xml) & "<br>"
on error resume next
paramname = param.selectsinglenode("name").text
if err.number <> 0 then
call responseerror("创建参数: 不能发现名称标签。")
end if
paramtype = param.selectsinglenode("type").text
paramdirection = param.selectsinglenode("direction").text
paramsize = param.selectsinglenode("size").text
paramvalue = param.selectsinglenode("value").text
if err.number <> 0 then
call responseerror("参数名为 '" & paramname & "'的参数缺少必要的域")
end if
cm.parameters.append cm.createparameter(paramname,paramtype,paramdirection,paramsize,paramvalue)
if err.number <> 0 then
call responseerror("不能创建或添加名为 '" & paramname & "的参数.' " & err.description)
response.end
end if
next
on error goto 0
end if
'打开连结
set conn = server.createobject("adodb.connection")
conn.mode=admodereadwrite
conn.open application("connectionstring")
if err.number <> 0 then
call responseerror("连结出错: " & err.description)
response.end
end if
' 连结command对象
set cm.activeconnection = conn
' 执行命令
if returnsdata then
' 用命令打开一个recordset
set r = server.createobject("adodb.recordset")
r.cursorlocation = aduseclient
r.open cm,,adopenstatic,adlockreadonly
else
cm.execute recordsaffected, ,adexecutenorecords
end if
if err.number <> 0 then
call responseerror("执行命令错误 '" & commandtext & "': " & err.description)
response.end
end if
if returnsdata then
r.save response, adpersistxml
if err.number <> 0 then
call responseerror("数据集发生存储错误,在命令'" & commandtext & "': " & err.description)
response.end
end if
elseif returnsvalues then
sxml = "<?xml version=""1.0"" encoding=""gb2312""?>" & vbcrlf & "<values>"
set nodes = xml.selectnodes("command/param[direction='2']")
for each n in nodes
nodename = n.selectsinglenode("name").text
sxml = sxml & "<" & nodename & ">" & cm.parameters(nodename).value & "" & "</" & nodename & ">"
next
sxml = sxml & "</values>"
response.write sxml
end if
set cm = nothing
conn.close
set r = nothing
set conn = nothing
response.end
%>