How to generate XML files dynamically using ASP

Source: Internet
Author: User
xml| Dynamic | Generate XML-related code as follows:
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
The relevant code is as follows:
<?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 (now) + "andfaculty=" + strfaculty+ "'")
Setmalerst=conn. Execute ("SelectCount (ID) asmalecountfromnewstudentwhererecruityear=" +cstr (now) + "andfaculty=" +strfaculty+ "' andgender= '")
Setfemalerst=conn. Execute ("SelectCount (ID) asfemalecountfromnewstudentwhererecruityear=" +cstr (now) + "andfaculty=" + strfaculty+ "' andgender= ' Woman")
%>
<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
%>



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.