SoFunction
Updated on 2025-04-13

ASP+XML instance drill programming code page 2/3


4. Description of Cls_Person class for operating Xml data ()

The Cls_Person class is used to complete various operations related to contact information, including adding, modifying, deleting, etc. It is written in VBScript. Cls_Person includes Id, Name, Nick, Mobile, Tel, Email, QQ and Company attributes, corresponding to the Person node in the XML file. Cls_Person includes four main methods: GetInfoFromXml, AddToXml, EditToXml and DeleteFormXml, which respectively complete the four functions of obtaining information, adding information, modifying information and deleting information.

The specific implementation of Cls_Person is as follows,
'*************************************************** 
' Instructions: Person class
' Author: gwd 2002-11-06
' Quote: pub/
'*************************************************** 
Class Cls_Person 
Private m_intId ' Id, corresponding to the position of Person node in the Persons collection
Private m_strName ' Name
Private m_strNick ' English name
Private m_strMobile ' Mobile
Private m_strTel ' Tel
Private m_strEmail ' Email
Private m_strQQ ' QQ Number
Private m_strCompany ' Company
Private m_strError ' Error message
' Class Initialization
Private Sub Class_Initialize() 
 m_strError = "" 
 m_intId = -1 
End Sub 
'Class release
Private Sub Class_Terminate() 
 m_strError = "" 
End Sub 
'----Read and write various attributes--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
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 
'----------------------------------------------- 
' Get the error message
Public Function GetLastError() 
 GetLastError = m_strError 
End Function 
' Private method, add error message
Private Sub AddErr(strEcho) 
 m_strError = m_strError + "<Div CLASS=""alert"">" & strEcho & "</Div>" 
End Sub 
'Clear the error message
Public Function ClearError() 
 m_strError = "" 
End Function 
' Read the data of the specified node from Xml and fill in each attribute
' Id needs to be set first
Public Function GetInfoFromXml(objXmlDoc) 
 Dim objNodeList 
 Dim I 
 ClearError 
 If objXmlDoc Is Nothing Then 
GetInfoFromXml = False 
AddErr "Dom object is empty"
Exit Function 
 End If 
 If CStr(m_intId) = "-1" Then 
GetInfoFromXml = False 
AddErr "The ID attribute of the contact object is not set correctly"
Exit Function 
 Else 
I = m_intId - 1 ' To read and get the node location
 End If 
'Select and read node information and assign various attributes
 Set objNodeList = ("Person") 
 If  - 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 "Error in obtaining contact information"
Set objNodeList = Nothing 
Exit Function 
 End If 
 Set objNodeList = Nothing 
End Function 
' Add information to the XML file
' The properties to be filled first need to be set
Public Function AddToXml(objXmlDoc) 
 Dim objPerson, objNode 
 ClearError 
 If objXmlDoc Is Nothing Then 
AddToXml = False 
AddErr "Dom object is empty"
Exit Function 
 End If 
'Create a Person node
 Set objPerson = ("Person") 
  objPerson 
'Create each child node
 '----------------------------------------------------- 
 Set objNode = ("Name") 
  = m_strName 
  objNode 
 Set objNode = ("Nick") 
  = m_strNick 
  objNode 
 Set objNode = ("Mobile") 
  = m_strMobile 
  objNode 
 Set objNode = ("Tel") 
  = m_strTel 
  objNode 
 Set objNode = ("Email") 
  = m_strEmail 
  objNode 
 Set objNode = ("QQ") 
  = m_strQQ 
  objNode 
 Set objNode = ("Company") 
  = m_strCompany 
  objNode 
 '----------------------------------------------------- 
 Set objNode = Nothing 
 Set objPerson = Nothing 
  On Error Resume Next 
(C_XMLFILE)'Save XML file
 If  = 0 Then 
AddToXml = True 
 Else 
AddToXml = False 
AddErr  
 End If 
End Function 
' Delete data from XML file
' Id needs to be set first
Public Function DeleteFromXml(objXmlDoc) 
 Dim objNodeList, objNode 
 ClearError 
 If objXmlDoc Is Nothing Then 
DeleteFromXml = False 
AddErr "Dom object is empty"
Exit Function 
 End If 
 If CStr(m_intId) = "-1" Then 
DeleteFromXml = False 
AddErr "The ID attribute of the contact object is not set correctly"
Exit Function 
 End If 
 Set objNodeList = ("Person") 
 If  - m_intId < 0 Then 
DeleteFromXml = False 
AddErr "No corresponding contact found"
Set objNodeList = Nothing 
Exit Function 
 End If 
 On Error Resume Next 
 Set objNode = (objNodeList(intId-1)) 
 If objNode Is Nothing Then 
DeleteFromXml = False 
AddErr "Delete contact failed"
Set objNodeList = Nothing 
Exit Function 
 Else 
 (C_XMLFILE) 
 End If 
 Set objNode = Nothing 
 Set objNodeList = Nothing 
 If  = 0 Then 
DeleteFromXml = True 
 Else 
DeleteFromXml = False 
AddErr  
 End If 
End Function 
'Modify the data in the XML file
' The ID needs to be set first
Public Function EditToXml(objXmlDoc) 
 Dim objPersonList, objOldPerson, objNewPerson, objNode 
 ClearError 
 If objXmlDoc Is Nothing Then 
EditToXml = False 
AddErr "Dom object is empty"
Exit Function 
 End If 
 If CStr(m_intId) = "-1" Then 
EditToXml = False 
AddErr "The ID attribute of the contact object is not set correctly"
Exit Function 
 End If 
 Set objPersonList = ("Person") 
 If  - m_intId < 0 Then 
DeleteFromXml = False 
AddErr "No corresponding contact found"
Set objPersonList = Nothing 
Exit Function 
 End If 
Set objOldPerson = objPersonList(m_intId-1) ' The old node to be modified
Set objNewPerson = ("Person") ' The new node used to replace the old node
 Set objNode = ("Name") 
  = m_strName 
  objNode 
 Set objNode = ("Nick") 
  = m_strNick 
  objNode 
 Set objNode = ("Mobile") 
  = m_strMobile 
  objNode 
 Set objNode = ("Tel") 
  = m_strTel 
  objNode 
 Set objNode = ("Email") 
  = m_strEmail 
  objNode 
 Set objNode = ("QQ") 
  = m_strQQ 
  objNode 
 Set objNode = ("Company") 
  = m_strCompany 
  objNode 
 On Error Resume Next 
'Change replacement
 Set objNode = (objNewPerson, objOldPerson) 
 If objNode Is Nothing Then 
 EditToXml = False 
AddErr "Failed to modify the contact"
 Set objOldPerosn = Nothing 
 Set objNewPerson = Nothing 
 Set objPersonList = Nothing 
 Exit Function 
Else 
  (C_XMLFILE) 
End If 
Set objOldPerson = Nothing 
Set objNewPerson = Nothing 
Set objPersonList = Nothing 
If  = 0 Then 
 EditToXml = True 
Else 
 EditToXml = False 
 AddErr  
End If 
End Function 
End Class 
Previous page123Next pageRead the full text