如何用Asp动态生成xml文件

80酷酷网    80kuku.com

  xml|动态|生成xml相关代码如下:
FunctionReplaceChar(FstrSource)
dimstrRet
ifIsNull(FstrSource)then
 FstrSource=""
endif
strRet=Replace(FstrSource,"&","&")
strRet=Replace(strRet,"<","<")
strRet=Replace(strRet,">",">")
strRet=Replace(strRet,"""",""")
strRet=Replace(strRet,"'","'")
ReplaceChar=strRet
EndFunction
functionRstToXML(FrstRst,FstrRstName)
dimstrSpace'spacestringbehandofelement
dimintLevel'leveloftheelement
dimstrXML'thereturnstring(xmlstring)
dimintRstField
dimstrShortDate

'documentlevel
intLevel=0
strSpace=space(intLevel*2)
ifLen(FstrRstName)>0then
strXML=strSpace&"<"&FstrRstName&">"&vbCR
intLevel=intLevel+1
strSpace=space(intLevel*2)
endif
ifFrstRst.EOFthen
strXML=strXML&strSpace&"<Record"
fornCount=0toFrstRst.Fields.Count-1
strXML=strXML&space(1)&FrstRst.Fields(nCount).Name&"=''"
next
strXML=strXML&"/>"&vbCR
ifLen(FstrRstName)>0then
strXML=strXML&strSpace&"</"&FstrRstName&">"&vbCR
endif
RstToXML=strXML
exitfunction
endif
'nowmoveinonelevel
intLevel=intLevel+1
strSpace=space(intLevel*2)

'loopthroughtherecords
dimstrTemp
FrstRst.MoveFirst
dowhilenotFrstRst.EOF
strTemp=""
'loopthroughthefields
'strXML=strXML&strSpace&"<Record"
foreachobjFieldinFrstRst.Fields
'setobjField=FrstRst.Fields(intRstField)
strTemp=strTemp&space(1)&objField.Name&"="
strTemp=strTemp&""""&ReplaceChar(objField.value)&""""
endif
next
strXML=strXML&"<Record"&strTemp&"/>"&vbCR
FrstRst.MoveNext
loop
intLevel=intLevel-1
strSpace=space(intLevel*2)
ifLen(FstrRstName)>0then
strXML=strXML&strSpace&"</"&FstrRstName&">"&vbCR
endif
RstToXML=strXML
endfunction
getInfo.asp
相关代码如下:
<?xmlversion="1.0"encoding="gb2312"?>
<root>
<%
setconn=server.CreateObject("ADODB.Connection")
conn.Open"FILEDSN=test.dsn"

setfacultyRst=conn.Execute("select*fromfaculty")
dowhilenotfacultyRst.eof
strFaculty=facultyRst("name")

setclassRst=conn.Execute("selectcount(id)asclasscountfromrecruitclasswhererecruityear="+cstr(year(now))+"andfaculty='"+strFaculty+"'")
setmaleRst=conn.Execute("selectcount(id)asmalecountfromnewstudentwhererecruityear="+cstr(year(now))+"andfaculty='"+strFaculty+"'andgender='男'")
setfemaleRst=conn.Execute("selectcount(id)asfemalecountfromnewstudentwhererecruityear="+cstr(year(now))+"andfaculty='"+strFaculty+"'andgender='女'")
%>
<newstudentfaculty="<%=strFaculty%>"class="<%=classRst("classcount")%>"male="<%=maleRst("malecount")%>"female="<%=femaleRst("femalecount")%>"/>
<%
facultyRst.MoveNext
loop
%>
</root>
<%
dimstrConn,strSQL,rs,n,sFileName
'changetheservername,ifitisremote,changetheUIDandPWDtoyourown
strConn="Provider=SQLOLEDB;Server=localhost;Database=pubs;UID=sa;PWD=;"
strSQL="SELECT*FROMemployee"
setrs=Server.CreateObject("ADODB.Recordset")
rs.openstrSQL,strConn,1,1
sFileName="c:\temp\employee.xml"
rs.savesFileName,1
rs.close
setrs=nothing
%>

or

<%
Response.ContentType="text/xml"
dimstrConn,strSQL,rs,n,sFileName
'changetheservername,ifitisremote,changetheUIDandPWDtoyourown
strConn="Provider=SQLOLEDB;Server=localhost;Database=pubs;UID=sa;PWD=;"
strSQL="SELECT*FROMemployee"
setrs=Server.CreateObject("ADODB.Recordset")
rs.openstrSQL,strConn,1,1
sFileName="c:\temp\employee.xml"
rs.saveResponse,1
rs.close
setrs=nothing
%>

分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: