XML(Extensible Markup Language)是一種基于文本格式的標(biāo)記語言,它注重對數(shù)據(jù)結(jié)構(gòu)和數(shù)據(jù)意義的描述,實(shí)現(xiàn)了數(shù)據(jù)內(nèi)容和顯示樣式的分離,而且是與平臺無關(guān)的。由于XML注重?cái)?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ì)算機(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ù)需要添加任意多的信息。
%@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>nbsp;/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>nbsp;/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>