请帮忙分析一段xmldom的程序!
以下两段程序看得不是太懂,请大家帮忙分析下其具体含义和作用:
rsxml.js
//============================================================================================================================
// The constructor function : create a httprequest object for maintain the database file through ADO
// Arguments :
// dsn : A string that specifies database connection. Required.
// sql : A string that specifies SQL command. Required.
// act : A character that identifies which action such as R-read, U-update, D-delete, A-add, S-execute. Required.
// data : An object that stores fieldname and fieldvalue when act is R/U/A.
// Returns :
// A string that specifies return status. if null menas normal otherwise stores error message.
//============================================================================================================================
function rsxml(dsn,sql,act,data,asp){
this.dsn = dsn;
this.sql = sql;
this.act = act;
if(data) this.data = data;
if(asp) this.asp = asp;
else this.asp= '/repsite/mysys/rsxml.asp ';
}
function rsxml_send(){
var s = ' <rsxml> </rsxml> ';
var xmldom = new ActiveXObject( "Microsoft.XMLDOM ");
xmldom.async = "false ";
xmldom.loadXML(s);
var n = xmldom.createElement( "dsn ");
n.text = this.dsn;
xmldom.documentElement.appendChild(n);
var n = xmldom.createElement( "sql ");
n.text = this.sql;
xmldom.documentElement.appendChild(n);
var n = xmldom.createElement( "act ");
n.text = this.act;
xmldom.documentElement.appendChild(n);
if(this.act== 'U '||this.act== 'A '){
var n = xmldom.createElement( "data ");
xmldom.documentElement.appendChild(n);
var xmldata = xmldom.getElementsByTagName( "data ").item(0);
for(var p in this.data){
var n = xmldom.createElement(p);
n.text = this.data[p];
xmldata.appendChild(n);
}
}
if(!xmldom.parseError.errorCode){
var x = new ActiveXObject( "Microsoft.XMLHTTP ");
x.open( "GET ",this.asp,false);
x.send(xmldom);
var y = x.responseXML;
if(x.status==200){
if(this.act== 'M '){
var z = y.documentElement.childNodes.item(0).childNodes
for(i=0;i <z.length;i++){
var xrow = new Object;
var r = z.item(i).childNodes
for(j=0;j <r.length;j++){
xrow[r.item(j).nodeName]=r.item(j).text;
}
this.data.push(xrow);
}
}
else{
for(i=0;i <y.documentElement.childNodes.length;i++){
if(y.documentElement.childNodes.item(i).nodeName!= 'data ')continue;
var z = y.documentElement.childNodes.item(i).childNodes
for(j=0;j <z.length;j++){
this.data[z.item(j).nodeName]=z.item(j).text;
}
}
}
return ' ';
}
else return 'Status code : ' + x.status + ' - ' + x.statusText;
}
else return 'XML Error : ' + xmldom.parseError.errorCode;
}
new rsxml();
rsxml.prototype.send = rsxml_send;
//============================================================================================================================
// The code above is the definition of the rsxml class.
//============================================================================================================================
rsxml.asp
<%
response.contenttype= "text/xml "
response.expires = 0
response.write " <?xml version= " "1.0 " " ?> "
response.write " <rsxml> "
set xmldom = server.createobject( "Microsoft.XMLDOM ")
xmldom.async = "false "
xmldom.load(request)
for each obj in xmldom.documentElement.childNodes
if obj.nodeName = "dsn " then mdsn = obj.text
if obj.nodeName = "sql " then msql = obj.text
if obj.nodeName = "act " then mact = obj.text
if obj.nodeName = "data " then set mdata = getdata(obj)
next
set xmldom = nothing
select case mact
case "R " : call rs_read(mdsn,msql)
case "U " : call rs_update(mdsn,msql,mdata) 'rs_test(mdata)
case "D " : call rs_delete(mdsn,msql)
case "A " : call rs_add(mdsn,msql,mdata)
case "S " : call rs_execute(mdsn,msql)
case "M " : call rs_multiread(mdsn,msql)
end select
response.write " </rsxml> "
response.end
sub rs_test(mdata)
response.write " <data> "
if isobject(mdata) then
response.write " <date> "&mdata( "date ")& " </date> "
response.write " <numeric> "&mdata( "numeric ")& " </numeric> "
response.write " <memo> "&mdata( "memo ")& " </memo> "
else
response.write " <character> ERROR </character> "
end if
response.write " </data> "
end sub
sub rs_read(mdsn,msql)
set rs = server.createObject( "adodb.recordset ")
with rs
.activeconnection = mdsn
.source = msql
.cursortype = 1
.cursorlocation = 3
.locktype = 1
.open
sxml = " <data> "
while not .eof
for each xfield in .fields
svalue = xfield.value
svalue = replace(svalue,CHR(38), "& ")
svalue = replace(svalue,CHR(34), "" ")
svalue = replace(svalue,CHR(39), "' ")
svalue = replace(svalue, " < ", "< ")
svalue = replace(svalue, "> ", "> ")
sxml = sxml & " < " & lcase(xfield.name) & "> "
sxml = sxml & svalue
sxml = sxml & " </ " & lcase(xfield.name) & "> "
next
.movenext
wend
sxml = sxml & " </data> "
.close
end with
set rs = nothing
call output(sxml)
end sub
sub rs_update(mdsn,msql,mdata)
set rs = server.createObject( "adodb.recordset ")
with rs
.activeconnection = mdsn
.source = msql
.cursortype = 3
.cursorlocation = 2
.locktype = 3
.open
if not .eof then call writedata(rs,mdata)
.close
end with
set rs = nothing
call output( " ")
end sub
sub rs_delete(mdsn,msql)
set rs = server.createObject( "adodb.recordset ")
with rs
.activeconnection = mdsn
.source = msql
.cursortype = 3
.cursorlocation = 2
.locktype = 3
.open
if not .eof then .delete
.close
end with
set rs = nothing
call output( " ")
end sub
sub rs_add(mdsn,msql,mdata)
set rs = server.createObject( "adodb.recordset ")
with rs
.activeconnection = mdsn
.source = msql
.cursortype = 3
.cursorlocation = 2
.locktype = 3
.open
.addnew
call writedata(rs,mdata)
.close
end with
set rs = nothing
call output( " ")
end sub
sub rs_execute(mdsn,msql)
set conn = server.createObject( "adodb.connection ")
conn.open mdsn
set rs = conn.execute(msql)
set rs = nothing
conn.close
set conn = nothing
call output( " ")
end sub
sub writedata(rs,mdata)
with rs
for i = 0 to .fields.count - 1
xfield = lcase(.fields(i).name)
if mdata.exists(xfield) then
xdata = mdata(xfield)
select case .fields(i).type
case 3,5,6,131
if xdata = " " then xdata = 0
.fields(i) = cdbl(xdata)
case 11
if xdata = " " then xdata = "false "
.fields(i) = cbool(xdata)
case else
.fields(i) = cstr(xdata)
end select
end if
next
.update
end with
end sub
sub rs_multiread(mdsn,msql)
set rs = server.createObject( "adodb.recordset ")
with rs
.activeconnection = mdsn
.source = msql
.cursortype = 1
.cursorlocation = 3
.locktype = 1
.open
sxml = " <data> "
while not .eof
sxml = sxml & " <row> "
for each xfield in .fields
svalue = xfield.value
svalue = replace(svalue,CHR(38), "& ")
svalue = replace(svalue,CHR(34), "" ")
svalue = replace(svalue,CHR(39), "' ")
svalue = replace(svalue, " < ", "< ")
svalue = replace(svalue, "> ", "> ")
sxml = sxml & " < " & lcase(xfield.name) & "> "
sxml = sxml & svalue
sxml = sxml & " </ " & lcase(xfield.name) & "> "
next
sxml = sxml & " </row> "
.movenext
wend
sxml = sxml & " </data> "
.close
end with
set rs = nothing
call output(sxml)
end sub
function getdata(obj)
set sd = server.createobject( "scripting.dictionary ")
for each xdata in obj.childNodes
sd.add xdata.nodeName, xdata.text
next
set getdata = sd
set sd = nothing
end function
sub output(sxml)
response.write sxml
end sub
%>
[解决办法]
UP~
