实例演练ASP+XML编程(3)
2018-09-06 12:01
四、操作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(标准化越来越近了)(objxml(标准化越来越近了)Doc)
Dim objNodeList
Dim I
ClearError
If objxml(标准化越来越近了)Doc 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 = objxml(标准化越来越近了)Doc.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(标准化越来越近了)(objxml(标准化越来越近了)Doc)
Dim objPerson, objNode
ClearError
If objxml(标准化越来越近了)Doc Is Nothing Then
AddToxml(标准化越来越近了) = False
AddErr Dom对象为空值
Exit Function
End If
创建Person节点
Set objPerson = objxml(标准化越来越近了)Doc.createElement(Person)
objxml(标准化越来越近了)Doc.documentElement.appendChild objPerson
创建各个子节点
-----------------------------------------------------
Set objNode = objxml(标准化越来越近了)Doc.createElement(Name)
objNode.Text = m_strName
objPerson.appendChild objNode
Set objNode = objxml(标准化越来越近了)Doc.createElement(Nick)
objNode.Text = m_strNick
objPerson.appendChild objNode
Set objNode = objxml(标准化越来越近了)Doc.createElement(Mobile)
objNode.Text = m_strMobile
objPerson.appendChild objNode
Set objNode = objxml(标准化越来越近了)Doc.createElement(Tel)
objNode.Text = m_strTel
objPerson.appendChild objNode
Set objNode = objxml(标准化越来越近了)Doc.createElement(Email)
objNode.Text = m_strEmail
objPerson.appendChild objNode
Set objNode = objxml(标准化越来越近了)Doc.createElement(QQ)
objNode.Text = m_strQQ
objPerson.appendChild objNode
Set objNode = objxml(标准化越来越近了)Doc.createElement(Company)
objNode.Text = m_strCompany
objPerson.appendChild objNode
-----------------------------------------------------
Set objNode = Nothing
Set objPerson = Nothing
On Error Resume Next
objxml(标准化越来越近了)Doc.save Server.MapPath(C_xml(标准化越来越近了)FILE) 保存xml(标准化越来越近了)文件
If Err.Number = 0 Then
AddToxml(标准化越来越近了) = True
Else
AddToxml(标准化越来越近了) = False
AddErr Err.Description
End If
End Function
从xml(标准化越来越近了)文件中删除数据
需要首先设置Id
Public Function DeleteFromxml(标准化越来越近了)(objxml(标准化越来越近了)Doc)
Dim objNodeList, objNode
ClearError
If objxml(标准化越来越近了)Doc Is Nothing Then
DeleteFromxml(标准化越来越近了) = False
AddErr Dom对象为空值
Exit Function
End If
If CStr(m_intId) = -1 Then
DeleteFromxml