[原创]ASP中RecordSet转换成XML文档并输出

 

XMLHelper类 完整代码

 

ContractedBlock.gif ExpandedBlockStart.gif Code
 1<%
 2    '--------------------------------------
 3    '类名称    :XML文档操作类
 4    '类用途    :提供XML文档的常用操作方法
 5    '类入口函数:InitClass
 6    '作者      :Kennyluo
 7    '版权所有  :珠海睿达信息科技有限公司
 8         '公司网址  :www.voswin.com
 9    '版本      :1.0
10    '版本更新  :2008-09-02
11    '--------------------------------------
12    class XMLHelper
13        private filename_
14        private xmlfile_
15        private objxml_
16        
17        property Let filename(value)
18            filename_=filename
19        end property
20        
21        property Get filename
22            filename =filename_
23        end property
24        
25        property Get objxml
26            objxml=objxml_
27        end property
28        
29        public function LoadXml(ByVal filename)
30            if filename <>"" then
31                filename_=filename
32            end if
33            xmlfile_=server.MapPath(filename_)
34            set objxml_=server.CreateObject("MSXML2.DOMDocument")
35            objxml_.load(sxmlfile)
36            objxml_.async=false
37            if objxml_.parseError.ErrorCode = 0 then
38                loadfile=true
39            else
40                CreateXMLFile
41            end if
42        end function
43        
44        private sub CreateXMLFile()
45            Set oPI=objxml_.createProcessingInstruction("xml""version=""1.0"" encoding=""GB2312""")
46            objxml_.insertBefore oPI,objxml_.childNodes(0
47            SaveXML
48        end sub
49        
50        private sub SaveXML()'保存文件
51            objxml_.save(xmlfile_)
52        end sub
53        
54        private sub SaveXMLForce()'保存文件
55            DelXML()
56            CreateXMLFile()
57            objxml_.save(xmlfile_)
58        end sub
59        private sub DelXML()
60            Set fso = Server.CreateObject("Scripting.FileSystemObject")
61            if fso.FileExists(xmlfile_) then                
62                fso.DeleteFile(xmlfile_)
63            end if
64            Set fso = nothing
65        end sub
66        public sub RecordSetToXML(ByVal Rs,ByVal ForceUpdate)
67            Dim objRootNode,objRowNode,objNode
68            Set objRootNode = objxml_.createElement("table"'创建根节点 
69            objxml_.documentElement = objRootNode
70            Do While Not Rs.Eof       '循环出所有记录 
71                Set objRowNode = objxml_.createElement("row"'创建父节点
72                 
73                for i = 0 to Rs.Fields.count - 1 '循环出所有字段
74                    Set objNode = objxml_.createElement(Rs.Fields(i).Name) '创建子节点 
75                    objNode.text = trim(Cstr(Rs.Fields(i).Value)) 
76                    objRowNode.appendChild(objNode)  
77                next
78                objRootNode.appendChild(objRowNode) 
79            Rs.MoveNext:Loop '循环结束
80            if ForceUpdate then 
81                 SaveXMLForce '强制输出XML文件
82            else
83                SaveXML '输出XML文件
84            end if
85            Set objNode = Nothing      '销毁对象 
86            Set objRowNode = Nothing  '销毁对象 
87            Set objRootNode = Nothing '销毁对象
88        end sub
89    end class
90%>

 

 

XMLHelper 类使用示例

 

 

 1  < ! -- #include file = " XMLHelper.asp " -->
 2  < %
 3           dim  xmlcontrol,sql
 4           Set  xmlcontrol  =   new  XMLHelper
 5           ' xmlcontrol.filename="xml.xml"
 6          xmlcontrol.LoadXml( " xml.xml " )
 7           ' --MsSQL 数据库连接--
 8          DBServer = " --- "    ' 数据库服务器IP地址
 9          DBuid = " ----- "            ' 数据库服务器用户名
10          DBpwd = " ------ "            数据库服务器用户密码
11          DBname = " -------- "      ' 数据库名称
12          
13          conndata = " driver={SQL Server};server= " & DBServer & " ;uid= " & DBuid & " ;pwd= " & DBpwd & " ;database= " & DBname & ""
14          
15           If   IsObject (conn)  =   false   Then
16               set  conn  =  Server.CreateObject( " ADODB.Connection " )
17              conn.Open conndata
18               If  Err.Number  <>   0   Then
19                 Response.Write(Err.Number)
20                 Response.write  " 系统日常维护中1! "
21                 Response.End()
22               end   if
23           end   if
24           Set  rs  =  Server.CreateObject( " adodb.recordset " )
25          sql  =   " 查询语句 "
26          rs.open sql,conn, 1 , 3
27           if   not  rs.eof  then
28              xmlcontrol.RecordSetToXML rs, false
29              Response.Write(rs.recordcount)
30           end   if
31          
32          rs.close
33           Set  rs  =   nothing
34          
35           if   IsObject (conn)  then
36              conn.close
37               Set  conn = nothing
38           end   if
39  % >

 

 

 

转载于:https://www.cnblogs.com/voswin/articles/1282493.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值