案例演練ASP+XML編程(4)
發表時間:2023-08-17 來源:明輝站整理相關軟件相關文章人氣:
[摘要]五、程序首頁(default.asp)調用相應的包含文件和公共函數,格式化XML文件,并進行顯示。可以看到,頁面Title是可定制的,公共的頭部和尾部都做成了相應的包含文件。C_TITLE、C_...
五、程序首頁(default.asp)
調用相應的包含文件和公共函數,格式化XML文件,并進行顯示。可以看到,頁面Title是可定制的,公共的頭部和尾部都做成了相應的包含文件。C_TITLE、C_XMLFILE和C_XSLFILE為公共常量,在constpub.asp文件中定義,至于它們的意義,相信讀者可以很容易地明白。這里調用了上面定義的FormatXml函數。 <% Option Explicit '*********************************************** ' 說明:通訊錄 ' 作者:gwd 2002-11-05 '*********************************************** %> <!--#include file="pub/funcxml.asp"--> <!--#include file="pub/constpub.asp"--> <HTML> <HEAD> <TITLE><% = C_TITLE %></TITLE> <META HTTP-EQUIV="content-type" CONTENT="text/html;charset=GB2312"/> <link rel="stylesheet" href="contact.css" type="text/css"> </HEAD> <BODY> <!--#include file="pub/header.asp"--> <% = FormatXml(C_XMLFILE, C_XSLFILE) %> <br> <!--#include file="pub/footer.asp"--> </BODY> </HTML> |
六、添加、修改和刪除XML中的信息
我們知道,在Cls_Person中已經定義了相應的方法,因此,在各個文件中,只需要調用對應的方法即可。添加信息的文件為add.asp,修改信息的文件為edit.asp,刪除信息的文件為delete.asp,我們僅以add.asp文件為例進行說明。其中的CheckStrInput和CheckStrOutput函數,用來格式化用戶的輸入和輸出字符串。
<% Option Explicit '*********************************************** ' 說明:37080308通訊錄 ' 作者:gwd 2002-11-05 '*********************************************** %> <!--#include file="pub/funcxml.asp"--> <!--#include file="pub/constpub.asp"--> <!--#include file="pub/funcpub.asp"--> <!--#include file="pub/class/clsPerson.asp"--> <% Dim objXml, objPerson Dim strErr Set objXml = Server.CreateObject("MSXML2.DOMDocument") Set objPerson = New Cls_Person ' 生成Cls_Person對象 If Request.Form("btnOk") <> "" Then If LoadXmlDoc(objXml, C_XMLFILE, False, strErr) Then ' 裝載XML文件 ' 給相應的屬性賦值 objPerson.Name = CheckStrInput(Request.Form("txtName")) objPerson.Nick = CheckStrInput(Request.Form("txtNick")) objPerson.Mobile = CheckStrInput(Request.Form("txtMobile")) objPerson.Tel = CheckStrInput(Request.Form("txtTel")) objPerson.Email = CheckStrInput(Request.Form("txtEmail")) objPerson.QQ = CheckStrInput(Request.Form("txtQQ")) objPerson.Company = CheckStrInput(Request.Form("txtCompany")) If Not objPerson.AddToXml(objXml) Then ' 調用Cls_Person類的AddToXml方法,添加數據 AddErr strErr, objPerson.GetLastError Else AddErr strErr, "添加成功" Response.Write "<script language=""javascript"">opener.location.reload();</script>" End If End If End If Set objXml = Nothing %> <HTML> <HEAD> <TITLE><% = C_TITLE %></TITLE> <META HTTP-EQUIV="content-type" CONTENT="text/html;charset=GB2312"/> <link rel="stylesheet" href="contact.css" type="text/css"> <script language="javascript"> <!-- function CheckForm() { return true; } //--> </script> </HEAD> <BODY> <% = strErr %> <div class="title">添加聯系信息</div> <form name="form1" method="post" action="add.asp" onsubmit="return CheckForm()"> <table align="center" width="100%" cellspacing="1" cellpadding="2" border="0" bgcolor="#666600"> <tr bgcolor="#ffffff"> <td width="25%" bgcolor="#e5e5e5" align="right"><b>姓名:</b></td> <td width="75%"><input type="text" name="txtName" size="25" class="input" value="<%=CheckStrOutput(objPerson.Name)%>"></td> </tr> <tr bgcolor="#ffffff"> <td bgcolor="#e5e5e5" align="right"><b>英文名:</b></td> <td><input type="text" name="txtNick" size="25" class="input" value="<%=CheckStrOutput(objPerson.Nick)%>"></td> </tr> <tr bgcolor="#ffffff"> <td bgcolor="#e5e5e5" align="right"><b>手機:</b></td> <td><input type="text" name="txtMobile" size="25" class="input" value="<%=CheckStrOutput(objPerson.Mobile)%>"></td> </tr> <tr bgcolor="#ffffff"> <td bgcolor="#e5e5e5" align="right"><b>電話:</b></td> <td><input type="text" name="txtTel" size="25" class="input" value="<%=CheckStrOutput(objPerson.Tel)%>"></td> </tr> <tr bgcolor="#ffffff"> <td bgcolor="#e5e5e5" align="right"><b>Email:</b></td> <td><input type="text" name="txtEmail" size="25" class="input" value="<%=CheckStrOutput(objPerson.Email)%>"></td> </tr> <tr bgcolor="#ffffff"> <td bgcolor="#e5e5e5" align="right"><b>QQ:</b></td> <td><input type="text" name="txtQQ" size="25" class="input" value="<%=CheckStrOutput(objPerson.QQ)%>"></td> </tr> <tr bgcolor="#ffffff"> <td bgcolor="#e5e5e5" align="right"><b>所在公司:</b></td> <td><input type="text" name="txtCompany" size="25" class="input" value="<%=CheckStrOutput(objPerson.Company)%>"></td> </tr> </table> <br> <div align="center"> <input type="submit" name="btnOk" value="提交"> <input type="button" name="btnClose" value="關閉" onclick="javascript:return window.close();"> </div> </form> </BODY> </HTML> <% Set objPerson = Nothing %> |
七、總結
到此,我們的聯系信息管理程序就大功告成了。怎么樣,感覺如何,應該來說還是相當簡單的吧。當然了,這個例程還有許多可以改進的地方,我這里也只不過是拋磚引玉,希望讀者在掌握了XML編程之后,自行修改完善吧。
此例程已經在我本機(Windows Server 2000、IIS5.0和IE6.0)和網上進行了測試,都能夠正常運行。
點擊下載本文源代碼