ASP创建xml、rss文件

conn.asp

<%
set conn=Server.CreateObject("ADODB.Connection")
DBPath=Server.MapPath("db1.mdb")
conn.Open "provider=microsoft.jet.oledb.4.0;data source="&dbpath
%>

Fso生成XML文件:Fso.xml

<%

Set fso=server.CreateObject("scripting.filesystemobject")

Set file=fso.createtextfile(server.mappath("Xml_File/Fso.xml"))

file.writeline("<?xml version=""1.0"" encoding=""gb2312""?>")

file.write("<students>")

For i=1 To 4

file.writeline("<student><name>victor" & i & "</name></student>")

next

file.writeline("</students>")

%>

<%response.write "成功!"%>

<br/>

<br/>

调用数据库数据生成XML文件:

第一种方法:生成Db1.xml

<!--#include file="conn.asp"-->

<%

Set rs=conn.execute("select *from article")

Dim id(10)

Dim title(10)

Dim author(10)

Dim content(10)

Dim datetime(10)

Do until rs.eof

id(line)=rs("id")

title(line)=rs("title")

author(line)=rs("author")

content(line)=rs("content")

datetime(line)=rs("datetime")

line=line+1

rs.movenext

Loop

%>

<%

Set fso=server.CreateObject("scripting.filesystemobject")

Set file=fso.createtextfile(server.mappath("Xml_File/Db1.xml"))

file.writeline("<?xml version=""1.0"" encoding=""gb2312""?>")

file.writeline("<books>")

For i=0 To line-1

file.writeline("<book>")

file.writeline("<id>" & id(i) & "</id>")

file.writeline("<title>" & title(i) & "</title>")

file.writeline("<author>" & author(i) & "</author>")

file.writeline("<content>" & content(i) & "</content>")

file.writeline("<datetime>" & datetime(i) & "</datetime>")

file.writeline("</book>")

next

file.writeline("</books>")

response.write "成功!"

%>

第二种方法:生成Db2.xml

<!--#include file="conn.asp"-->

<%

Set rs=server.CreateObject("adodb.recordset")

rs.open "select *from article",conn,1,1

Dim idd(10)

Dim titlee(10)

Dim authorr(10)

Dim contentt(10)

Dim datetimee(10)

For linee=1 To rs.recordcount

idd(linee)=rs("id")

titlee(linee)=rs("title")

authorr(linee)=rs("author")

contentt(linee)=rs("content")

datetimee(linee)=rs("datetime")

linee=linee+1

rs.movenext

Next

%>

<%

Set fso=server.CreateObject("scripting.filesystemobject")

Set file=fso.createtextfile(server.mappath("Xml_File/Db2.xml"))

file.writeline("<?xml version=""1.0"" encoding=""gb2312""?>")

file.writeline("<books>")

For j=1 To linee-1

file.writeline("<book>")

file.writeline("<id>" & idd(j) & "</id>")

file.writeline("<title>" & titlee(j) & "</title>")

file.writeline("<author>" & authorr(j) & "</author>")

file.writeline("<content>" & contentt(j) & "</content>")

file.writeline("<datetime>" & datetimee(j) & "</datetime>")

file.writeline("</book>")

next

file.writeline("</books>")

response.write "成功!"

%>

<br/>

<br/>

创建RSS文件:生成Rss.xml

<!--#include file="conn.asp"-->

<%

Set rs=conn.execute("select *from article")

Dim id2(2)

Dim title2(2)

Dim author2(2)

Dim content2(2)

Dim datetime2(2)

Do until rs.eof

id2(line2)=rs("id")

title2(line2)=rs("title")

author2(line2)=rs("author")

content2(line2)=rs("content")

datetime2(line2)=rs("datetime")

line2=line2+1

rs.movenext

Loop

%>

<%

Set fso=server.CreateObject("scripting.filesystemobject")

Set file=fso.createtextfile(server.mappath("Xml_File/Rss.xml"))

LineHead="<?xml version=""1.0"" encoding=""gb2312""?>" & vbcrlf

  LineHead=LineHead & "<rss version=""2.0"" xmlns:dc=""http://purl.org/dc/elements/1.1/"" "

  LineHead=LineHead & "xmlns:trackback=""http://madskills.com/public/xml/rss/module/trackback/"" "

  LineHead=LineHead & "xmlns:wfw=""http://wellformedweb.org/CommentAPI/"" "

  LineHead=LineHead & "xmlns:slash=""http://purl.org/rss/1.0/modules/slash/"" "

  LineHead=LineHead & "xmlns:content=""http://purl.org/rss/1.0/modules/content/"">"

  file.writeline(LineHead)

For i=0 To line2-1

file.writeline("<book>")

file.writeline("<id>" & id2(i) & "</id>")

file.writeline("<title>" & title2(i) & "</title>")

file.writeline("<author>" & author2(i) & "</author>")

file.writeline("<content>" & content2(i) & "</content>")

file.writeline("<datetime>" & datetime2(i) & "</datetime>")

file.writeline("</book>")

next  

file.writeline("</rss>")

response.write("成功!")

%>

    A+
发布日期:2007年04月04日  所属分类:未分类

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: