ASP開發(fā)基于XML的留言板
XML(Extensible Markup Language)是一種基于文本格式的標(biāo)記語言,它注重對數(shù)據(jù)結(jié)構(gòu)和數(shù)據(jù)意義的描述,實(shí)現(xiàn)了數(shù)據(jù)內(nèi)容和顯示樣式的分離,而且是與平臺無關(guān)的。由于XML注重數(shù)據(jù)內(nèi)容的描述,因而,對于數(shù)據(jù)的檢索非常有意義,我們不會再象HTML那樣,檢索出與我們要求無關(guān)的信息。另一方面,XML文件是數(shù)據(jù)的載體,利用XML作為數(shù)據(jù)庫,不需要訪問任何數(shù)據(jù)庫系統(tǒng),我們可以使用任意WEB技術(shù)來顯示我們的數(shù)據(jù),比如HTML,F(xiàn)lash 5 等。由于世界各大計算機(jī)公司的積極參與,XML正日益成為基于互聯(lián)網(wǎng)的數(shù)據(jù)格式新一代的標(biāo)準(zhǔn)。下面利用XML作為數(shù)據(jù)的載體,開發(fā)一個基于XML的留言板。
首先,我們建立XML文件guestbook.xml,該文件記錄了留言者的姓名、電子郵件、網(wǎng)址、留言內(nèi)容。當(dāng)然,我們也可以根據(jù)需要添加任意多的信息。
文件內(nèi)容如下:
<?xml version="1.0" encoding="gb2312"?> <留言本> <留言記錄> <留言者姓名>孟憲會</留言者姓名> <電子郵件>amxh@testDomain.com</電子郵件> <網(wǎng)址>http://go.163.com/~colorweb</網(wǎng)址> <留言內(nèi)容>測試成功??!</留言內(nèi)容> </留言記錄> </留言本>
由于目前許多服務(wù)器都支持ASP,我們采用常見的ASP來作為實(shí)現(xiàn)的工具,guestbook.asp文件如下:
<%@Language="VBScript"%> <% '設(shè)置Web頁面的信息 Response.Buffer = true Response.Expires = -1 '顯示留言函數(shù)init() Function init() entryForm() '定義局部變量 Dim objXML Dim arrNames Dim arrEmails Dim arrURLS Dim arrMessages '創(chuàng)建XMLDOM文檔對象,用來存放留言 Set objXML = server.createObject("Msxml2.DOMDocument") objXML.async = false objXML.load(server.MapPath("guestbook.xml")) '取得留言本各元素的集合 Set arrNames = objXML.getElementsByTagName("留言者姓名") Set arrEmails = objXML.getElementsByTagName("電子郵件") Set arrURLS = objXML.getElementsByTagName("網(wǎng)址") Set arrMessages = objXML.getElementsByTagName("留言內(nèi)容") Response.Write "<table border='0' width='100%'>" Response.Write "<tr><td bgcolor='#00CCFF' align='center' height='26'>" Response.Write "<b>各位的留言如下:</b>" Response.Write "</td></tr>" '輸出留言本各元素的內(nèi)容,最新的留言先顯示 For x=arrNames.length-1 To 0 Step -1 Response.Write "<tr><td><a href=mailto:" & arrEmails.item(x).text & ">" & arrNames.item(x).text & "</a></td></tr>" Response.Write "<tr><td>網(wǎng)址:<a href=" & arrURLS.item(x).text & " target='_blank'>" & arrURLS.item(x).text & "</a><td></tr>" Response.Write "<tr><td>留言內(nèi)容:</td></tr>" Response.Write "<tr><td bgcolor='#0099ff'>" & arrMessages.item(x).text &"</td></tr>" Response.Write "<tr><td> </td></tr>" Next Response.Write "</table>" Set objXML = nothing End Function '向XML文件添加留言記錄的函數(shù)addEntry() Function addEntry() '定義局部變量 Dim strName Dim strEmail Dim strURL Dim strMessage '取得留言表單的輸入內(nèi)容 strName = Request.Form("姓名") strEmail = Request.Form("電子郵件") strURL = Request.Form("網(wǎng)址") strMessage = Request.Form("留言") Dim objXML Dim objEntry Dim objName Dim objEmail Dim objURL Dim objMessage '向XML文件添加留言內(nèi)容 Set objXML = server.createObject("Msxml2.DOMDocument") objXML.async = false objXML.load(server.MapPath("guestbook.xml")) Set objEntry = objXML.createNode("element", "留言記錄", "") objXML.documentElement.appendChild(objEntry) Set objName = objXML.createNode("element", "留言者姓名", "") objEntry.appendChild(objName) objName.text = strName Set objEmail = objXML.createNode("element", "電子郵件", "") objEntry.appendChild(objEmail) objEmail.text = strEmail Set objURL = objXML.createNode("element", "網(wǎng)址", "") objEntry.appendChild(objURL) objURL.text = strURL Set objMessage = objXML.createNode("element", "留言內(nèi)容", "") objEntry.appendChild(objMessage) objMessage.text = strMessage objXML.save(server.MapPath("guestbook.xml")) Response.Redirect("guestbook.asp") End function '填寫和發(fā)送留言表單的函數(shù)entryForm() Function entryForm() Response.Write "<p align='center'><b>XML 留言本 例子</b></p>" Response.Write "<hr color='#000099' width='100%' noshade>" Response.Write "<form action=guestbook.asp?action=addEntry method=post>" Response.Write "<table border=1>" Response.Write "<tr><td>您的姓名:</td><td><input type=text name=姓名 /></td></tr>" Response.Write "<tr><td>電子郵件:</td><td><input type=text name=電子郵件 /></td></tr>" Response.Write "<tr><td>您的網(wǎng)址:</td><td><input type=text name=網(wǎng)址 /></td></tr>" Response.Write "<tr><td>您的留言:</td><td><textarea name=留言 cols=40 rows=5></textarea></td></tr>" Response.Write "<tr><td> </td><td><input type=submit value=發(fā)布留言 /></td></tr>" Response.Write "</table>" Response.Write "</form>" End Function %> <html> <head> <title>XML 留言例子</title> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> </head> <body> <% '判斷是否發(fā)送了留言,并更新留言信息 Dim a a = Request.Querystring("action") If a<>"" Then addEntry else init End If %> </body> </html>
以上是利用XML開發(fā)留言板簡單的例子,可以根據(jù)需要進(jìn)行添加更多的功能。
- ajax 入門基礎(chǔ)之 XMLHttpRequest對象總結(jié)
- LINQ to XML的編程基礎(chǔ)
- PHP中開發(fā)XML應(yīng)用程序之基礎(chǔ)篇 添加節(jié)點(diǎn) 刪除節(jié)點(diǎn) 查詢節(jié)點(diǎn) 查詢節(jié)
- 服務(wù)器XMLHTTP(Server XMLHTTP in ASP)基礎(chǔ)知識
- android開發(fā)基礎(chǔ)教程—三種方式實(shí)現(xiàn)xml文件解析
- C# XML與Json之間相互轉(zhuǎn)換實(shí)例詳解
- jQuery處理xml格式的返回數(shù)據(jù)(實(shí)例解析)
- DOM基礎(chǔ)及php讀取xml內(nèi)容操作的方法
- C#簡單寫入xml文件的方法
- C#寫入對象或集合類型數(shù)據(jù)到xml文件的方法
- C#實(shí)現(xiàn)的XML操作類實(shí)例
- asp控制xml數(shù)據(jù)庫的經(jīng)典代碼
- 初識XML基礎(chǔ)知識
相關(guān)文章
檢查access數(shù)據(jù)庫中是否存在某個名字的表的asp代碼
首先調(diào)用adodb.connection對象中的openSchema函數(shù),這樣會得到一個Recordset,其中每一條“紀(jì)錄”對應(yīng)著數(shù)據(jù)庫中的一張表,“紀(jì)錄”的每個“字段”包含了對應(yīng)表的某方面信息。其中TABLE_NAME字段包含了對應(yīng)表的名稱2009-06-06服務(wù)端 VBScript 與 JScript 幾個相同特性的寫法與示例
服務(wù)端 VBScript 與 JScript 幾個相同特性的寫法與示例...2007-03-03動網(wǎng)論壇的asp 數(shù)據(jù)庫連接代碼
動網(wǎng)論壇的asp程序,在一定程度了,成了asp的頂峰之作。高手2009-02-02CreateKeyWord asp實(shí)現(xiàn)的由給定的字符串生成關(guān)鍵字的代碼
CreateKeyWord asp實(shí)現(xiàn)的由給定的字符串生成關(guān)鍵字的代碼...2007-09-09