四、操作xml数据的cls_person类说明(clsperson.asp)
cls_person类用来完成与联系人信息相关的各种操作,包括添加、修改和删除等等,它使用vbscript编写。cls_person包括id、name、nick、mobile、tel、email、qq和company属性,对应于xml文件中的person节点。cls_person包括getinfofromxml、addtoxml、edittoxml和deleteformxml四个主要方法,分别完成获取信息,添加信息,修改信息和删除信息四个功能。
cls_person的具体实现如下,
***************************************************
说明:person类
作者:gwd 2002-11-06
引用:pub/constpub.asp
***************************************************
class cls_person
private m_intid id,对应person节点在persons集合中的位置
private m_strname 姓名
private m_strnick 英文名
private m_strmobile 手机
private m_strtel 电话
private m_stremail 电子邮件
private m_strqq qq号
private m_strcompany 所在公司
private m_strerror 出错信息
类初始化
private sub class_initialize()
m_strerror = “”
m_intid = -1
end sub
类释放
private sub class_terminate()
m_strerror = “”
end sub
—–读写各个属性—————————
public property get id
id = m_intid
end property
public property let id(intid)
m_intid = intid
end property
public property get name
name = m_strname
end property
public property let name(strname)
m_strname = strname
end property
public property get nick
nick = m_strnick
end property
public property let nick(strnick)
m_strnick = strnick
end property
public property get mobile
mobile = m_strmobile
end property
public property let mobile(strmobile)
m_strmobile = strmobile
end property
public property get tel
tel = m_strtel
end property
public property let tel(strtel)
m_strtel = strtel
end property
public property get email
email = m_stremail
end property
public property let email(stremail)
m_stremail = stremail
end property
public property get qq
qq = m_strqq
end property
public property let qq(strqq)
m_strqq = strqq
end property
public property get company
company = m_strcompany
end property
public property let company(strcompany)
m_strcompany = strcompany
end property
———————————————–
获取错误信息
public function getlasterror()
getlasterror = m_strerror
end function
私有方法,添加错误信息
private sub adderr(strecho)
m_strerror = m_strerror + “<div class=””alert””>” & strecho & “</div>”
end sub
清除错误信息
public function clearerror()
m_strerror = “”
end function
从xml中读取指定节点的数据,并填充各个属性
需要首先设置id
public function getinfofromxml(objxmldoc)
dim objnodelist
dim i
clearerror
if objxmldoc is nothing then
getinfofromxml = false
adderr “dom对象为空值”
exit function
end if
if cstr(m_intid) = “-1” then
getinfofromxml = false
adderr “未正确设置联系人对象的id属性”
exit function
else
i = m_intid – 1 要读取得节点位置
end if
选择并读取节点信息,赋予各个属性
set objnodelist = objxmldoc.getelementsbytagname(“person”)
if objnodelist.length – m_intid >= 0 then
on error resume next
m_strname = objnodelist(i).selectsinglenode(“name”).text
m_strnick = objnodelist(i).selectsinglenode(“nick”).text
m_strmobile = objnodelist(i).selectsinglenode(“mobile”).text
m_strtel = objnodelist(i).selectsinglenode(“tel”).text
m_stremail = objnodelist(i).selectsinglenode(“email”).text
m_strqq = objnodelist(i).selectsinglenode(“qq”).text
m_strcompany = objnodelist(i).selectsinglenode(“company”).text
getinfofromxml = true
else
getinfofromxml = false
adderr “获取联系信息发生错误”
set objnodelist = nothing
exit function
end if
set objnodelist = nothing
end function
添加信息到xml文件中
需要首先设置好要填充的属性
public function addtoxml(objxmldoc)
dim objperson, objnode
clearerror
if objxmldoc is nothing then
addtoxml = false
adderr “dom对象为空值”
exit function
end if
创建person节点
set objperson = objxmldoc.createelement(“person”)
objxmldoc.documentelement.appendchild objperson
创建各个子节点
—————————————————–
set objnode = objxmldoc.createelement(“name”)
objnode.text = m_strname
objperson.appendchild objnode
set objnode = objxmldoc.createelement(“nick”)
objnode.text = m_strnick
objperson.appendchild objnode
set objnode = objxmldoc.createelement(“mobile”)
objnode.text = m_strmobile
objperson.appendchild objnode
set objnode = objxmldoc.createelement(“tel”)
objnode.text = m_strtel
objperson.appendchild objnode
set objnode = objxmldoc.createelement(“email”)
objnode.text = m_stremail
objperson.appendchild objnode
set objnode = objxmldoc.createelement(“qq”)
objnode.text = m_strqq
objperson.appendchild objnode
set objnode = objxmldoc.createelement(“company”)
objnode.text = m_strcompany
objperson.appendchild objnode
—————————————————–
set objnode = nothing
set objperson = nothing
on error resume next
objxmldoc.save server.mappath(c_xmlfile) 保存xml文件
if err.number = 0 then
addtoxml = true
else
addtoxml = false
adderr err.description
end if
end function
从xml文件中删除数据
需要首先设置id
public function deletefromxml(objxmldoc)
dim objnodelist, objnode
clearerror
if objxmldoc is nothing then
deletefromxml = false
adderr “dom对象为空值”
exit function
end if
if cstr(m_intid) = “-1” then
deletefromxml = false
adderr “未正确设置联系人对象的id属性”
exit function
end if
set objnodelist = objxmldoc.getelementsbytagname(“person”)
if objnodelist.length – m_intid < 0 then
deletefromxml = false
adderr “未找到相应的联系人”
set objnodelist = nothing
exit function
end if
on error resume next
set objnode = objxmldoc.documentelement.removechild(objnodelist(intid-1))
if objnode is nothing then
deletefromxml = false
adderr “删除联系人失败”
set objnodelist = nothing
exit function
else
objxmldoc.save server.mappath(c_xmlfile)
end if
set objnode = nothing
set objnodelist = nothing
if err.number = 0 then
deletefromxml = true
else
deletefromxml = false
adderr err.description
end if
end function
修改xml文件中的数据
需要首先设置好id
public function edittoxml(objxmldoc)
dim objpersonlist, objoldperson, objnewperson, objnode
clearerror
if objxmldoc is nothing then
edittoxml = false
adderr “dom对象为空值”
exit function
end if
if cstr(m_intid) = “-1” then
edittoxml = false
adderr “未正确设置联系人对象的id属性”
exit function
end if
set objpersonlist = objxmldoc.getelementsbytagname(“person”)
if objpersonlist.length – m_intid < 0 then
deletefromxml = false
adderr “未找到相应的联系人”
set objpersonlist = nothing
exit function
end if
set objoldperson = objpersonlist(m_intid-1) 要修改的旧节点
set objnewperson = objxmldoc.createelement(“person”) 用来替换旧节点的新节点
set objnode = objxmldoc.createelement(“name”)
objnode.text = m_strname
objnewperson.appendchild objnode
set objnode = objxmldoc.createelement(“nick”)
objnode.text = m_strnick
objnewperson.appendchild objnode
set objnode = objxmldoc.createelement(“mobile”)
objnode.text = m_strmobile
objnewperson.appendchild objnode
set objnode = objxmldoc.createelement(“tel”)
objnode.text = m_strtel
objnewperson.appendchild objnode
set objnode = objxmldoc.createelement(“email”)
objnode.text = m_stremail
objnewperson.appendchild objnode
set objnode = objxmldoc.createelement(“qq”)
objnode.text = m_strqq
objnewperson.appendchild objnode
set objnode = objxmldoc.createelement(“company”)
objnode.text = m_strcompany
objnewperson.appendchild objnode
on error resume next
进行替换
set objnode = objxmldoc.documentelement.replacechild(objnewperson, objoldperson)
if objnode is nothing then
edittoxml = false
adderr “修改联系人失败”
set objoldperosn = nothing
set objnewperson = nothing
set objpersonlist = nothing
exit function
else
objxmldoc.save server.mappath(c_xmlfile)
end if
set objoldperson = nothing
set objnewperson = nothing
set objpersonlist = nothing
if err.number = 0 then
edittoxml = true
else
edittoxml = false
adderr err.description
end if
end function
end class