目前在使用XML文件的过程中,常常要用到动态的XML文件,比如RSS源就是一种动态的XML文件,在这里介绍一种通过ASP实现的输出动态XML格式文件最简单的方法:
在ASP文件的最首行,加条语句:Response.ContentType = "text/XML"即可.
例子如下:
数据库表图:
代码如下:
<%
Response.ContentType = "text/XML"
Dim sql,rs,dd,conn
sql="select * from tb_book "
dd="Provider=microsoft.jet.oledb.4.0;data source="&Server.MapPath("xml.mdb")
set conn=Server.CreateObject("ADODB.Connection")
conn.connectionstring=dd
conn.open
Set rs=conn.execute(sql)
%>
<?xml version="1.0" encoding="gb2312" ?>
<bookinfo>
<% Do While Not rs.eof%>
<book>
<title><%= rs("title")%></title>
<author><% =rs("author")%></author>
<publish>
<publisher><%=rs(" publisher")%></publisher>
<ISBN><%= rs("ISBN")%></ISBN>
</publish>
<price><%= rs("price")%></price>
</book>
<%
rs.movenext
Loop
Set rs=Nothing
conn.close
%>
</bookinfo>
Response.ContentType = "text/XML"
Dim sql,rs,dd,conn
sql="select * from tb_book "
dd="Provider=microsoft.jet.oledb.4.0;data source="&Server.MapPath("xml.mdb")
set conn=Server.CreateObject("ADODB.Connection")
conn.connectionstring=dd
conn.open
Set rs=conn.execute(sql)
%>
<?xml version="1.0" encoding="gb2312" ?>
<bookinfo>
<% Do While Not rs.eof%>
<book>
<title><%= rs("title")%></title>
<author><% =rs("author")%></author>
<publish>
<publisher><%=rs(" publisher")%></publisher>
<ISBN><%= rs("ISBN")%></ISBN>
</publish>
<price><%= rs("price")%></price>
</book>
<%
rs.movenext
Loop
Set rs=Nothing
conn.close
%>
</bookinfo>
效果如图:
作者:mrlelong