【ASP】輸出XML(不產生檔案)

將資料庫內容匯出成XML格式,但不產生檔案。可以應用在Ajax上存取資料之用途。

<%
   response.CharSet ="utf-8"
   session("dsn")="dsn=Northwind;uid=sa;pwd=SQLsys"

   set rs=server.CreateObject ("adodb.recordset")
   sqlstr="select 客戶編號,公司名稱,連絡人,城市,電話 from dbo.客戶"
   rs.Open sqlstr,session("dsn"),3,1,1
  
   Response.Clear()

   if not rs.EOF then
        With Response
            .ContentType = "text/XML"
            .write("<?xml version=""1.0"" encoding=""utf-8""?>")
            .write("<customers>")
        End with
     do while not rs.eof
         response.write("<customer>")
        for each x in rs.fields  '讀取所有欄位
          With Response
             .write("<" & x.name & ">")   '欄位名稱            
             .write(x.value)              '欄位值
             .write("</" & x.name & ">")         
          End with         
        next
        response.write("</customer>")
     rs.movenext
     loop
      response.write("</customers>")
   end if
   rs.Close

   Response.End()        
%>

 輸出畫面 :

留言

這個網誌中的熱門文章

【ASP】日期轉換(西元<-->民國)

【VB】使用NPOI元件來匯出Excel--DataTableToExcel

【SQL】符號切割字串變成多欄