<%
function wawa_recordxml(sql)
*************************
功能:把recordset转换成xml格式的字符串
返回值:字符串
参数:sql(字符串)
提供者:蛙蛙王子(天极论坛)
*************************
dim rs,strxml
strxml=””
strxml=strxml&”<?xml version=1.0 encoding=gb2312?>”&vbcrlf
strxml=strxml&”<wawa>”
set rs= server.createobject(“adodb.recordset”)
rs.open sql,conn,1,1
if not(rs.eof and rs.bof) then
do while not rs.eof
dim i
for i = 0 to rs.fields.count – 1
strxml=strxml&” <“&rs.fields(i).name&”>”&wawa_xml_text(rs.fields(i).value)&”</”&rs.fields(i).name&”>”&vbcrlf
next
rs.movenext
loop
strxml=strxml&”</wawa>”
wawa_recordxml=strxml
else
end if
rs.close
set rs=nothing
end function
%>
<%
function wawa_createxml(strxml)
*************************
功能:把符合xml格式的字符串写在服务器的一个目录上
返回值:无
参数:strxml(字符串)
提供者:蛙蛙王子(天极论坛)
*************************
dim objxml,fs,dir,files,path
set fs = createobject(“scripting.filesystemobject”)
dir=server.mappath(“xml”)
if (fs.folderexists(dir)) then
else
fs.createfolder(dir)
end if
files=”wawa.xml”
path=dir&”\”&files
set fs=nothing
set objxml = server.createobject(“msxml2.domdocument”)
objxml.validateonparse = true
objxml.async=false
objxml.loadxml(strxml)
if objxml.parseerror.errorcode <> 0 then
response.write(“error: ” & objxml.parseerror.reason & “<br>”)
response.write(“code: 0x” & hex(objxml.parseerror.errorcode) & “<br>”)
response.write(“at line: ” & objxml.parseerror.line & “<br>”)
response.write(“at pos: ” & objxml.parseerror.linepos & “<br>”)
else
set objrootelement = objxml.documentelement
if not isobject(objrootelement) then
response.write(“no file loaded”)
else
response.write(strxml)
objxml.save path
end if
end if
end function
%>
<%
function wawa_xml_text(fstring)
*************************
功能:把一些特殊字符替换成转换符,以便让xml的text节点合法
返回值:字符串
参数:fstring(字符串)
提供者:蛙蛙王子(天极论坛)
*************************
if fstring<>”” then
fstring=cstr(fstring)
fstring = replace(fstring, “&”,”&”)
fstring = replace(fstring, “<“,”<”)
fstring = replace(fstring, “>”,”>”)
fstring = replace(fstring, chr(34), “"”) 双引号
fstring = replace(fstring, chr(39), “'”) 单引号
wawa_xml_text = fstring
end if
end function
%>
<!– 使用方法如下:conn.asp文件自己写就可以了,但数据库连接对象实例的名字必须是conn,然后把上面的三个函数保存为一个vbsxml.asp
并包含进来,然后就是写自己所需要的sql字符串并调用函数了,函数里可能会有一些小bug,比如说rs.field.name里面”xml”,生成的xml
文件就不合法了,时间太短,不写了,这些bug由自己去保证不出错吧,呵呵
–>
<!–#include file=”conn.asp” –>
<!–#include file=”vbsxml.asp” –>
<%
response.contenttype = “text/xml”
sql=”select lei_id as 编号 ,lei_name as 城市 from tese_lei order by lei_id desc”
call wawa_createxml(wawa_recordxml(sql))
%>
