五、程序首页(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 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' 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' 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' 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' 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' 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' 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' 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)和网上进行了测试,都能够正常运行。