如何用Asp動態產生xml檔案

來源:互聯網
上載者:User
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
%>



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.