[收藏]如何用Asp动态生成xml文件

2005/6/19 12:55:12


阅读全文(1954) | 回复(0) | 编辑 | 精华

相关代码如下:Function ReplaceChar ( FstrSource )dim strRetif IsNull(FstrSource) then&n bsp; FstrSource = ""end ifstrRet = Replace ( FstrSource , "&" , "&" )strRet = Replace ( strRet , "<" , "<" )strRet = Replace ( strRet , ">" , ">" )strRet = Replace ( strRet , """" , """ )strRet = Replace ( strRet , "'" , "'" )ReplaceChar = strRetEnd Functionfunction RstToXML (FrstRst, FstrRstName)dim strSpace 'space string behand of elementdim intLevel 'level of the elementdim strXML 'the return string(xml string)dim intRstFielddim strShortDate'document levelintLevel = 0strSpace = space (intLevel * 2)if Len(FstrRstName)>0 thenstrXML=strSpace & "<" & FstrRstName & ">" & vbCRintLevel = intLevel + 1strSpace = space(intLevel*2)end ifif FrstRst.EOF thenstrXML = strXML&strSpace & "<Record"for nCount=0 to FrstRst.Fields.Count-1strXML = strXML & space(1)&FrstRst.Fields(nCount).Name&"=''"nextstrXML = strXML & "/>" &vbCRif Len(FstrRstName)>0 thenstrXML=strXML&strSpace & "</" & FstrRstName & ">" & vbCRend ifRstToXML=strXMLexit functionend if' now move in one levelintLevel = intLevel + 1strSpace = space (intLevel * 2 )' loop through the recordsdim strTempFrstRst.MoveFirstdo while not FrstRst.EOFstrTemp = ""'loop through the fields'strXML = strXML & strSpace & "<Record"for each objField in FrstRst.Fields'set objField = FrstRst.Fields(intRstField)strTemp = strTemp & space (1) & objField.Name & "=" strTemp = strTemp & """" & ReplaceChar(objField.value) & """"end ifnextstrXML = strXML & "<Record "&strTemp& "/>" & vbCRFrstRst.MoveNext loopintLevel=intLevel-1strSpace=space(intLevel * 2)if Len(FstrRstName)>0 thenstrXML = strXML & strSpace & "</" & FstrRstName & ">" & vbCRend ifRstToXML = strXMLend functiongetInfo.asp相关代码如下:<?xml version="1.0" encoding="gb2312"?><root><%set conn = server.CreateOb ject("ADODB.Connection")conn.Open "FILEDSN=test.dsn" set facultyRst = conn.Execute("select * from faculty")do while not facultyRst.eofstrFaculty = facultyRst("name")set classRst = conn.Execute("select count(id) as classcount from recruitclass where recruityear=" + cstr(year(now)) + " and faculty='" + strFaculty + "'")set maleRst = conn.Execute("select count(id) as malecount from newstudent where recruityear=" + cstr(year(now)) + " and faculty='" + strFaculty + "' and gender='男'")set femaleRst = conn.Execute("select count(id) as femalecount from newstudent where recruityear=" + cstr(year(now)) + " and faculty='" + strFaculty + "' and gender='女'")%><newstudent faculty="<%=strFaculty%>" class="<%=classRst("classcount")%>" male="<%=maleRst("malecount")%>" female="<%=femaleRst("femalecount")%>"/><%facultyRst.MoveNextloop%></root><%dim strConn, strSQL, rs, n, sFileNa me'change the server name, if it is remote, change the UID and PWD to your ownstrConn = "Provider=SQLOLEDB;Server=localhost;Database=pubs;UID=sa;PWD=;"strSQL = "SELECT * FROM employee"set rs = Server.CreateObject("ADODB.Recordset")rs.open strSQL, strConn, 1, 1sFileName = "c:\temp\employee.xml"rs.save sFileName, 1rs.closeset rs =nothing%> or <%Response.ContentType = "text/xml"dim strConn, strSQL, rs, n, sFileName'change the server name, if it is remote, change the UID and PWD to your ownstrConn = "Provider=SQLOLEDB;Server=localhost;Database=pubs;UID=sa;PWD=;"strSQL = "SELECT * FROM employee"set rs = Server.CreateObject("ADODB.Recordset")rs.open strSQL, strConn, 1, 1sFileName = "c:\temp\employee.xml"rs.save Response, 1rs.closeset rs =nothing%>[引:中国ASP论坛]<完>参与讨论本主题

Qr

Posted by Qr on 2005/6/19 12:55:12


发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)
站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.063 second(s), page refreshed 144817512 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号