<p> 四、操作Xml数据的Cls_Person类说明(clsPerson.asp)</p><p> Cls_Person类用来完成与联系人信息相关的各种操作,包括添加、修改和删除等等,它使用VBScript编写。Cls_Person包括Id、Name、Nick、Mobile、Tel、Email、QQ和Company属性,对应于XML文件中的Person节点。Cls_Person包括GetInfoFromXml、AddToXml、EditToXml和DeleteFormXml四个主要方法,分别完成获取信息,添加信息,修改信息和删除信息四个功能。</p><p> Cls_Person的具体实现如下,</p><p> <code>'***************************************************<br />' 说明:Person类<br />' 作者:gwd 2002-11-06<br />' 引用:pub/constpub.asp<br />'***************************************************<br />Class Cls_Person<br />Private m_intId ' Id,对应Person节点在Persons集合中的位置<br />Private m_strName ' 姓名<br />Private m_strNick ' 英文名<br />Private m_strMobile ' 手机<br />Private m_strTel ' 电话<br />Private m_strEmail ' 电子邮件<br />Private m_strQQ ' QQ号<br />Private m_strCompany ' 所在公司<br />Private m_strError ' 出错信息<br />' 类初始化<br />Private Sub Class_Initialize()<br /> m_strError = ""<br /> m_intId = -1<br />End Sub<br />' 类释放<br />Private Sub Class_Terminate()<br /> m_strError = ""<br />End Sub<br />'-----读写各个属性---------------------------<br />Public Property Get Id<br /> Id = m_intId<br />End Property<br />Public Property Let Id(intId)<br /> m_intId = intId<br />End Property<br />Public Property Get Name<br />Name = m_strName<br />End Property<br />Public Property Let Name(strName)<br /> m_strName = strName<br />End Property<br />Public Property Get Nick<br /> Nick = m_strNick<br />End Property<br />Public Property Let Nick(strNick)<br /> m_strNick = strNick<br />End Property<br />Public Property Get Mobile<br /> Mobile = m_strMobile<br />End Property<br />Public Property Let Mobile(strMobile)<br /> m_strMobile = strMobile<br />End Property<br />Public Property Get Tel<br /> Tel = m_strTel<br />End Property<br />Public Property Let Tel(strTel)<br /> m_strTel = strTel<br />End Property<br />Public Property Get Email<br /> Email = m_strEmail<br />End Property<br />Public Property Let Email(strEmail)<br /> m_strEmail = strEmail<br />End Property<br />Public Property Get QQ<br /> QQ = m_strQQ<br />End Property<br />Public Property Let QQ(strQQ)<br /> m_strQQ = strQQ<br />End Property<br />Public Property Get Company<br /> Company = m_strCompany<br />End Property<br />Public Property Let Company(strCompany)<br /> m_strCompany = strCompany<br />End Property<br />'-----------------------------------------------<br />' 获取错误信息<br />Public Function GetLastError()<br /> GetLastError = m_strError<br />End Function<br />' 私有方法,添加错误信息<br />Private Sub AddErr(strEcho)<br /> m_strError = m_strError + "<Div CLASS=""alert"">" & strEcho & "</Div>"<br />End Sub<br />' 清除错误信息<br />Public Function ClearError()<br /> m_strError = ""<br />End Function<br />' 从Xml中读取指定节点的数据,并填充各个属性<br />' 需要首先设置Id<br />Public Function GetInfoFromXml(objXmlDoc)<br /> Dim objNodeList<br /> Dim I<br />ClearError<br />If objXmlDoc Is Nothing Then<br /> GetInfoFromXml = False<br /> AddErr "Dom对象为空值"<br /> Exit Function<br /> End If<br />If CStr(m_intId) = "-1" Then<br /> GetInfoFromXml = False<br /> AddErr "未正确设置联系人对象的ID属性"<br /> Exit Function<br /> Else<br /> I = m_intId - 1 ' 要读取得节点位置<br /> End If<br />' 选择并读取节点信息,赋予各个属性<br /> Set objNodeList = objXmlDoc.getElementsByTagName("Person")<br /> If objNodeList.length - m_intId >= 0 Then<br /> On Error Resume Next<br /> m_strName = objNodeList(I).selectSingleNode("Name").Text<br /> m_strNick = objNodeList(I).selectSingleNode("Nick").Text<br /> m_strMobile = objNodeList(I).selectSingleNode("Mobile").Text<br /> m_strTel = objNodeList(I).selectSingleNode("Tel").Text<br /> m_strEmail = objNodeList(I).selectSingleNode("Email").Text<br /> m_strQQ = objNodeList(I).selectSingleNode("QQ").Text<br /> m_strCompany = objNodeList(I).selectSingleNode("Company").Text<br /> GetInfoFromXml = True<br /> Else<br /> GetInfoFromXml = False<br /> AddErr "获取联系信息发生错误"<br /> Set objNodeList = Nothing<br /> Exit Function<br /> End If<br /> Set objNodeList = Nothing<br />End Function<br />' 添加信息到XML文件中<br />' 需要首先设置好要填充的属性<br />Public Function AddToXml(objXmlDoc)<br /> Dim objPerson, objNode<br />ClearError<br />If objXmlDoc Is Nothing Then<br /> AddToXml = False<br /> AddErr "Dom对象为空值"<br /> Exit Function<br /> End If<br />' 创建Person节点<br /> Set objPerson = objXmlDoc.createElement("Person")<br /> objXmlDoc.documentElement.appendChild objPerson<br />' 创建各个子节点<br /> '-----------------------------------------------------<br /> Set objNode = objXmlDoc.createElement("Name")<br /> objNode.Text = m_strName<br /> objPerson.appendChild objNode<br />Set objNode = objXmlDoc.createElement("Nick")<br /> objNode.Text = m_strNick<br /> objPerson.appendChild objNode<br />Set objNode = objXmlDoc.createElement("Mobile")<br /> objNode.Text = m_strMobile<br /> objPerson.appendChild objNode<br />Set objNode = objXmlDoc.createElement("Tel")<br /> objNode.Text = m_strTel<br /> objPerson.appendChild objNode<br />Set objNode = objXmlDoc.createElement("Email")<br /> objNode.Text = m_strEmail<br /> objPerson.appendChild objNode<br />Set objNode = objXmlDoc.createElement("QQ")<br /> objNode.Text = m_strQQ<br /> objPerson.appendChild objNode<br />Set objNode = objXmlDoc.createElement("Company")<br /> objNode.Text = m_strCompany<br /> objPerson.appendChild objNode<br /> '-----------------------------------------------------<br />Set objNode = Nothing<br /> Set objPerson = Nothing<br />On Error Resume Next<br /> objXmlDoc.save Server.MapPath(C_XMLFILE) '保存XML文件<br /> If Err.Number = 0 Then<br /> AddToXml = True<br /> Else<br /> AddToXml = False<br /> AddErr Err.Description<br /> End If<br />End Function<br />' 从XML文件中删除数据<br />' 需要首先设置Id<br />Public Function DeleteFromXml(objXmlDoc)<br /> Dim objNodeList, objNode<br />ClearError<br />If objXmlDoc Is Nothing Then<br /> DeleteFromXml = False<br /> AddErr "Dom对象为空值"<br /> Exit Function<br /> End If<br />If CStr(m_intId) = "-1" Then<br /> DeleteFromXml = False<br /> AddErr "未正确设置联系人对象的ID属性"<br /> Exit Function<br /> End If<br />Set objNodeList = objXmlDoc.getElementsByTagName("Person")<br /> If objNodeList.length - m_intId < 0 Then<br /> DeleteFromXml = False<br /> AddErr "未找到相应的联系人"<br /> Set objNodeList = Nothing<br /> Exit Function<br /> End If<br />On Error Resume Next<br /> Set objNode = objXmlDoc.documentElement.removeChild(objNodeList(intId-1))<br /> If objNode Is Nothing Then<br /> DeleteFromXml = False<br /> AddErr "删除联系人失败"<br /> Set objNodeList = Nothing<br /> Exit Function<br /> Else<br /> objXmlDoc.save Server.MapPath(C_XMLFILE)<br /> End If<br /> Set objNode = Nothing<br /> Set objNodeList = Nothing<br />If Err.Number = 0 Then<br /> DeleteFromXml = True<br /> Else<br /> DeleteFromXml = False<br /> AddErr Err.Description<br /> End If<br />End Function<br />' 修改XML文件中的数据<br />' 需要首先设置好Id<br />Public Function EditToXml(objXmlDoc)<br /> Dim objPersonList, objOldPerson, objNewPerson, objNode<br />ClearError<br />If objXmlDoc Is Nothing Then<br /> EditToXml = False<br /> AddErr "Dom对象为空值"<br /> Exit Function<br /> End If<br />If CStr(m_intId) = "-1" Then<br /> EditToXml = False<br /> AddErr "未正确设置联系人对象的ID属性"<br /> Exit Function<br /> End If<br />Set objPersonList = objXmlDoc.getElementsByTagName("Person")<br /> If objPersonList.length - m_intId < 0 Then<br /> DeleteFromXml = False<br /> AddErr "未找到相应的联系人"<br /> Set objPersonList = Nothing<br /> Exit Function<br /> End If<br />Set objOldPerson = objPersonList(m_intId-1) ' 要修改的旧节点<br />Set objNewPerson = objXmlDoc.createElement("Person") ' 用来替换旧节点的新节点<br /> Set objNode = objXmlDoc.createElement("Name")<br /> objNode.Text = m_strName<br /> objNewPerson.appendChild objNode<br />Set objNode = objXmlDoc.createElement("Nick")<br /> objNode.Text = m_strNick<br /> objNewPerson.appendChild objNode<br />Set objNode = objXmlDoc.createElement("Mobile")<br /> objNode.Text = m_strMobile<br /> objNewPerson.appendChild objNode<br />Set objNode = objXmlDoc.createElement("Tel")<br /> objNode.Text = m_strTel<br /> objNewPerson.appendChild objNode<br />Set objNode = objXmlDoc.createElement("Email")<br /> objNode.Text = m_strEmail<br /> objNewPerson.appendChild objNode<br />Set objNode = objXmlDoc.createElement("QQ")<br /> objNode.Text = m_strQQ<br /> objNewPerson.appendChild objNode<br />Set objNode = objXmlDoc.createElement("Company")<br /> objNode.Text = m_strCompany<br /> objNewPerson.appendChild objNode<br />On Error Resume Next<br /> ' 进行替换<br /> Set objNode = objXmlDoc.documentElement.replaceChild(objNewPerson, objOldPerson)<br /> If objNode Is Nothing Then<br /> EditToXml = False<br /> AddErr "修改联系人失败"<br /> Set objOldPerosn = Nothing<br /> Set objNewPerson = Nothing<br /> Set objPersonList = Nothing<br /> Exit Function<br />Else<br /> objXmlDoc.save Server.MapPath(C_XMLFILE)<br />End If<br />Set objOldPerson = Nothing<br />Set objNewPerson = Nothing<br />Set objPersonList = Nothing<br />If Err.Number = 0 Then<br /> EditToXml = True<br />Else<br /> EditToXml = False<br /> AddErr Err.Description<br />End If<br />End Function<br />End Class</code></p>
<p> </p>
<p> 五、程序首页(default.asp)</p><p> 调用相应的包含文件和公共函数,格式化XML文件,并进行显示。可以看到,页面Title是可定制的,公共的头部和尾部都做成了相应的包含文件。C_TITLE、C_XMLFILE和C_XSLFILE为公共常量,在constpub.asp文件中定义,至于它们的意义,相信读者可以很容易地明白。这里调用了上面定义的FormatXml函数。</p><p> <code><% Option Explicit<br />'***********************************************<br />' 说明:通讯录<br />' 作者:gwd 2002-11-05<br />'***********************************************<br />%><br /><!--#include file="pub/funcxml.asp"--><br /><!--#include file="pub/constpub.asp"--><br /><HTML><br /><HEAD><br /><TITLE><% = C_TITLE %></TITLE><br /><META HTTP-EQUIV="content-type" CONTENT="text/html;charset=GB2312"/><br /><link rel="stylesheet" href="contact.css" type="text/css"><br /></HEAD><br /><BODY><br /><!--#include file="pub/header.asp"--><br /><% = FormatXml(C_XMLFILE, C_XSLFILE) %><br /><br><br /><!--#include file="pub/footer.asp"--><br /></BODY><br /></HTML><br /></code> </p><p> 六、添加、修改和删除XML中的信息</p><p> 我们知道,在Cls_Person中已经定义了相应的方法,因此,在各个文件中,只需要调用对应的方法即可。添加信息的文件为add.asp,修改信息的文件为edit.asp,删除信息的文件为delete.asp,我们仅以add.asp文件为例进行说明。其中的CheckStrInput和CheckStrOutput函数,用来格式化用户的输入和输出字符串。</p>
<p> </p>
<p> <code><% Option Explicit<br />'***********************************************<br />' 说明:37080308通讯录<br />' 作者:gwd 2002-11-05<br />'***********************************************<br />%><br /><!--#include file="pub/funcxml.asp"--><br /><!--#include file="pub/constpub.asp"--><br /><!--#include file="pub/funcpub.asp"--><br /><!--#include file="pub/class/clsPerson.asp"--><br /><%<br /> Dim objXml, objPerson<br /> Dim strErr<br />Set objXml = Server.CreateObject("MSXML2.DOMDocument")<br /> Set objPerson = New Cls_Person ' 生成Cls_Person对象<br /> If Request.Form("btnOk") <> "" Then<br /> If LoadXmlDoc(objXml, C_XMLFILE, False, strErr) Then ' 装载XML文件<br /> ' 给相应的属性赋值<br /> objPerson.Name = CheckStrInput(Request.Form("txtName"))<br /> objPerson.Nick = CheckStrInput(Request.Form("txtNick"))<br /> objPerson.Mobile = CheckStrInput(Request.Form("txtMobile"))<br /> objPerson.Tel = CheckStrInput(Request.Form("txtTel"))<br /> objPerson.Email = CheckStrInput(Request.Form("txtEmail"))<br /> objPerson.QQ = CheckStrInput(Request.Form("txtQQ"))<br /> objPerson.Company = CheckStrInput(Request.Form("txtCompany"))<br /> If Not objPerson.AddToXml(objXml) Then ' 调用Cls_Person类的AddToXml方法,添加数据<br /> AddErr strErr, objPerson.GetLastError<br /> Else<br /> AddErr strErr, "添加成功"<br /> Response.Write "<script language=""javascript"">opener.location.reload();</script>"<br /> End If<br /> End If<br /> End If<br /> Set objXml = Nothing<br />%><br /><HTML><br /><HEAD><br /><TITLE><% = C_TITLE %></TITLE><br /><META HTTP-EQUIV="content-type" CONTENT="text/html;charset=GB2312"/><br /><link rel="stylesheet" href="contact.css" type="text/css"><br /><script language="javascript"><br /><!--<br />function CheckForm()<br />{<br /> return true;<br />}<br />//--><br /></script><br /></HEAD><br /><BODY><br /><% = strErr %><br /><div class="title">添加联系信息</div><br /><form name="form1" method="post" action="add.asp" onsubmit="return CheckForm()"><br /> <table align="center" width="100%" cellspacing="1" cellpadding="2" border="0" bgcolor="#666600"><br /> <tr bgcolor="#ffffff"><br /> <td width="25%" bgcolor="#e5e5e5" align="right"><b>姓名:</b></td><br /> <td width="75%"><input type="text" name="txtName" size="25" class="input" value="<%=CheckStrOutput(objPerson.Name)%>"></td><br /> </tr><br /> <tr bgcolor="#ffffff"><br /> <td bgcolor="#e5e5e5" align="right"><b>英文名:</b></td><br /> <td><input type="text" name="txtNick" size="25" class="input" value="<%=CheckStrOutput(objPerson.Nick)%>"></td><br /> </tr><br /> <tr bgcolor="#ffffff"><br /> <td bgcolor="#e5e5e5" align="right"><b>手机:</b></td><br /> <td><input type="text" name="txtMobile" size="25" class="input" value="<%=CheckStrOutput(objPerson.Mobile)%>"></td><br /> </tr><br /> <tr bgcolor="#ffffff"><br /> <td bgcolor="#e5e5e5" align="right"><b>电话:</b></td><br /> <td><input type="text" name="txtTel" size="25" class="input" value="<%=CheckStrOutput(objPerson.Tel)%>"></td><br /> </tr><br /> <tr bgcolor="#ffffff"><br /> <td bgcolor="#e5e5e5" align="right"><b>Email:</b></td><br /> <td><input type="text" name="txtEmail" size="25" class="input" value="<%=CheckStrOutput(objPerson.Email)%>"></td><br /> </tr><br /> <tr bgcolor="#ffffff"><br /> <td bgcolor="#e5e5e5" align="right"><b>QQ:</b></td><br /> <td><input type="text" name="txtQQ" size="25" class="input" value="<%=CheckStrOutput(objPerson.QQ)%>"></td><br /> </tr><br /> <tr bgcolor="#ffffff"><br /> <td bgcolor="#e5e5e5" align="right"><b>所在公司:</b></td><br /> <td><input type="text" name="txtCompany" size="25" class="input" value="<%=CheckStrOutput(objPerson.Company)%>"></td><br /> </tr><br /> </table><br /> <br><br /> <div align="center"><br /> <input type="submit" name="btnOk" value="提交"><br /> <input type="button" name="btnClose" value="关闭" onclick="javascript:return window.close();"><br /> </div><br /></form><br /></BODY><br /></HTML><br /><%<br />Set objPerson = Nothing<br />%><br /></code> </p></p><p> 七、总结</p><p> 到此,我们的联系信息管理程序就大功告成了。怎么样,感觉如何,应该来说还是相当简单的吧。当然了,这个例程还有许多可以改进的地方,我这里也只不过是抛砖引玉,希望读者在掌握了XML编程之后,自行修改完善吧。</p><p> 此例程已经在我本机(Windows Server 2000、IIS5.0和IE6.0)和网上进行了测试,都能够正常运行。</p></p>