Asp+|xml| programming
This article is an example to explain the article. As an ordinary programmer, I am well aware that an excellent routine is very helpful to those who are learning to program. The routines used in this article are a contact information management program that I also write to make it easy for me to connect with my friends. But though small, spite, believe to be learning Asp+xml programming friends, or have a certain reference value.
Readers can use this example to learn how to manipulate XML files in an ASP (Active Server Page) and perform various processing of data, including building, modifying, deleting, and saving XML nodes, and so on. The techniques involved include asp,vbscript,dom,xml and XSL.
This article does not carry on the in-depth theory introduction to the use technology, therefore, the reader needs to have certain relevant knowledge, especially to ASP, XML and Dom should have certain understanding. Read through this article, and refer to the source code, I believe that the reader must be proficient in mastering XML programming.
I. Description of the procedure
The example is based on B/s structure, uses the XML file to store the contact information, and then uses the DOM to perform various operations on the contact information in the XML file through a class written in VBScript.
The code provided by the example uses a uniform naming convention, which includes: a three-letter abbreviation for variable types, such as numeric type--int, String type--str, Object--obj, and so on, although in Asp/vbscript, the data type is not distinguished, but with the obvious data type description , the programming and maintenance of the program is still very meaningful; Use meaningful variable names, such as XmlDocument objects, defined as objXMLDoc, and so on, as well as doing so to better write and maintain programs.
This program can be divided into background data processing and front interface performance two parts.
Program backstage, using VBScript to write a class, this is in the VBScript5.0 version of the new features provided. Although the concept of class here and the real object-oriented, but the rational use of class in the ASP, or to a certain extent to improve the operation of the program efficiency and maintainability.
The foreground performance, using XSL to format the data in the XML file, and then outputting it to the client in HTML, fully embodies the flexibility and customization of XML technology. The format of the process is placed on the server side, using an ASP program to complete, so that the client is the formatted HTML information to avoid the emergence of compatibility issues.
Of course, procedures for specific operational details are not very rigorous testing, such as contact information required to check, but for the ASP in the use of DOM to manipulate the relevant parts of XML, the program provides a complete sample code.
ii. XML Document Description (Persons.xml)
The XML file structure used in this routine is very simple and does not have a schema or DTD defined, because it is unnecessary for this program. Of course, if the reader is willing to define one of its own, it will not affect the operation of the program.
The data structure of the program is defined as follows, the persons collection, which contains multiple person objects, each of whom includes name name, English Nick, mobile phone, telephone tel, email email, Tencent QQ, and the company's properties. The above definition corresponds to an XML file that is, persons is the root node, and person is the child of the persons, Name, Nick, Mobile, Tel, Email, QQ, and company are child nodes of person.
Thus, the contents of the XML file we get are as follows:
£
Small East
GWd
139XXXXXXXX
XXXXXXXX
gwd@chinaren.com
7066015
XXX
£
Readers need to pay attention to this line, XML does not support Chinese by default, and you can make XML display Chinese correctly by setting the Encoding property. Readers can access this file in IE5.0 and above browsers, and it will display the data in a tree-shaped structure.
Iii. format Conversion XSL file description (persons.xsl)
The XML data is formatted using XSL in the process and returned to the client in HTML form. This process can also be done on the client side, but given the compatibility problem, the routine uses the ASP to manipulate the DOM on the server side to format the method.
The contents of the xsl file are as follows,
<?xml version= "1.0" encoding= "gb2312"?
<xsl:stylesheet xmlns:xsl= "Http://www.w3.org/1999/XSL/Transform" version= "1.0"
<xsl:template match= "/persons"
<table width= "border=" "0" align= "center"
<tr>
<TD align= "Right" ><a href= Javascript:add (); "title= Add New Contact" > add new contact </a> </td>
</tr>
</table>
<table align= "center" width= "680" cellspacing= "1" cellpadding= "2" border= "0" bgcolor= "#666600"
<TR class= "title" Bgcolor= "#E5E5E5"
<TD width= "><xsl:text" disable-output-escaping= "yes" >& </xsl:text> nbsp; </td>
<td> name </td>
<td> English name </td>
<td> Mobile </td>
<td> Phone </td>
<td> Email </td>
<td> QQ </td>
<td> your company </td>
</tr>
<xsl:for-each select= "Person" >
<TR bgcolor= "#FFFFFF" >
<TD align= "right" ><xsl:value-of select= "position ()"/> </TD>
<TD style= "COLOR: #990000" > <A> <xsl:attribute name= "HREF" >javascript:edit (' <xsl:value-of select= "position ()"/> "); </ Xsl:attribute><xsl:attribute name= "title" Modify information </xsl:attribute> <xsl:value-of select= "name"/> </A> </TD>
<TD> <xsl:value-of select= "Nick"/> </TD>
<TD> <xsl:value-of select= "Mobile"/> </TD>
<TD> <xsl:value-of select= "Tel"/> </TD>
<TD> <A> <xsl:attribute name= "HREF" >mailto:<xsl:value-of select= "Email"/> </xsl:attribute> <xsl:value-of select= " Email "/> </A> </TD>
<TD> <xsl:value-of select= "QQ"/> </TD>
<TD> <xsl:value-of select= "Company"/> </TD>
</TR>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
The conversion on the server side uses a function to complete, format successfully, return the HTML string, format failed, print out the error message, such as the following,
'*******************************************
' Description: Format the XML file with the XSL file.
' Parameters: Strxmlfile--XML file, path + filename
' Strxslfile--xsl file, path + filename
' Return: Success--formatted HTML string
' Failure--Custom error message
'*******************************************
Function Formatxml (Strxmlfile, Strxslfile)
Dim Objxml, objxsl
Strxmlfile = Server.MapPath (strxmlfile)
Strxslfile = Server.MapPath (strxslfile)
Set objxml = Server.CreateObject ("MSXML2. DOMDocument ")
Set objxsl = Server.CreateObject ("MSXML2. DOMDocument ")
Objxml.async = False
If objxml.load (strxmlfile) Then
Objxsl.async = False
Objxsl.validateonparse = False
If objxsl.load (strxslfile) Then
On error Resume Next ' captures Transformnode method errors
Formatxml = Objxml.transformnode (objxsl)
If objXsl.parseError.errorCode <> 0 Then
Response.Write "<br> Response.Write "Error Code:" & ObjXsl.parseError.errorCode
Response.Write "<br> Error Reason:" & ObjXsl.parseError.reason
Response.Write "<br> Error Line:" & ObjXsl.parseError.line
Formatxml = "<span class=" "Alert" "> format XML file Error!" </span> the
End If
Else
Response.Write "<br> Response.Write "Error Code:" & ObjXsl.parseError.errorCode
Response.Write "<br> Error Reason:" & ObjXsl.parseError.reason
Response.Write "<br> Error Line:" & ObjXsl.parseError.line
Formatxml = "<span class=" "Alert" "> loading XSL file error!" </span> the
End If
Else
Response.Write "<br> Response.Write "Error Code:" & ObjXml.parseError.errorCode
Response.Write "<br> Error Reason:" & ObjXml.parseError.reason
Response.Write "<br> Error Line:" & ObjXml.parseError.line
Formatxml = "<span class=" "Alert" "> loading XML file Error!" </span> the
End If
Set objxsl = Nothing
Set Objxml = Nothing
End Function
Iv. Cls_person class Description of manipulating XML data (clsperson.asp)
The cls_person class is used to complete a variety of actions related to contact information, including additions, modifications, deletions, and so on, which are 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 of Getinfofromxml, Addtoxml, Edittoxml and Deleteformxml, respectively, to obtain information, add information, modify information and delete information four functions.
The specific implementation of cls_person is as follows,
'***************************************************
' References: pub/constpub.asp
'***************************************************
Class Cls_person
Private m_intid ' Id, corresponding to the position of the person node in the persons collection
Private M_strname ' name
Private M_strnick ' English name
Private m_strmobile ' Mobile
Private M_strtel ' phone
Private m_stremail ' e-mail
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 releases
Private Sub Class_Terminate ()
M_strerror = ""
End Sub
'-----Read and write to each property---------------------------
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, adding error messages
Private Sub Adderr (Strecho)
M_strerror = M_strerror + "<div class=" "Alert" ">" & Strecho & "</Div>"
End Sub
' Clear error message
Public Function ClearError ()
M_strerror = ""
End Function
' Reads the data of the specified node from the XML and populates the individual properties
' You need to set the ID first
Public Function Getinfofromxml (objXMLDoc)
Dim objNodeList
Dim I
ClearError
If objXMLDoc is nothing Then
Getinfofromxml = False
Adderr "DOM object is null"
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 to get the node position
End If
' Select and read node information, giving individual attributes
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 "Get Contact information Error"
Set objnodelist = Nothing
Exit Function
End If
Set objnodelist = Nothing
End Function
' Add information to the XML file
' You need to set the properties to be populated first.
Public Function Addtoxml (objXMLDoc)
Dim Objperson, Objnode
ClearError
If objXMLDoc is nothing Then
Addtoxml = False
Adderr "DOM object is null"
Exit Function
End If
' Create a person node
Set Objperson = objxmldoc.createelement ("person")
ObjXmlDoc.documentElement.appendChild Objperson
' Create each child node
'-----------------------------------------------------
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) ' Save XML file
If Err.Number = 0 Then
Addtoxml = True
Else
Addtoxml = False
Adderr Err.Description
End If
End Function
' Delete data from the XML file
' You need to set the ID first
Public Function Deletefromxml (objXMLDoc)
Dim objNodeList, Objnode
ClearError
If objXMLDoc is nothing Then
Deletefromxml = False
Adderr "DOM object is null"
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 = objxmldoc.getelementsbytagname ("person")
If Objnodelist.length-m_intid 0 Then
Deletefromxml = False
Adderr "no corresponding contact found"
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 "Delete Contact failed"
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
' Modify the data in the XML file
' You need to set the ID first.
Public Function Edittoxml (objXMLDoc)
Dim objpersonlist, Objoldperson, Objnewperson, Objnode
ClearError
If objXMLDoc is nothing Then
Edittoxml = False
Adderr "DOM object is null"
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 = objxmldoc.getelementsbytagname ("person")
If Objpersonlist.length-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 modify
Set Objnewperson = objxmldoc.createelement ("person") is used to replace the new node of the old node
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
' To replace
Set objnode = ObjXmlDoc.documentElement.replaceChild (Objnewperson, Objoldperson)
If Objnode is nothing Then
Edittoxml = False
Adderr "Modify Contact Failed"
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
V. Program homepage (default.asp)
Call the corresponding include file and public function, format the XML file, and display it. As you can see, the title of the page is customizable, and the public head and tail are made up of the corresponding include files. C_title, C_xmlfile, and c_xslfile are common constants, defined in constpub.asp files, and as far as their meaning is concerned, readers can easily understand them. This invokes the Formatxml function defined above.
<% Option Explicit
%>
!--#include file= "pub/funcxml.asp"-->
!--#include file= "pub/constpub.asp"-->
<% = C_title%>
!--#include file= "pub/header.asp"-->
<% = Formatxml (C_xmlfile, C_xslfile)%>
!--#include file= "pub/footer.asp"-->
Vi. adding, modifying, and deleting information in XML
We know that the corresponding method has been defined in the Cls_person, so in each file, you just need to call the corresponding method. The file to add the information is add.asp, the file that modifies the information is edit.asp, the file that deletes the information is delete.asp, we only take add.asp file as an example to explain. The Checkstrinput and Checkstroutput functions are used to format the user's input and output strings.
<% Option Explicit
%>
!--#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 ' generates Cls_person object
If Request.Form ("Btnok") <> "Then
If Loadxmldoc (Objxml, C_xmlfile, False, strerr) Then ' Load XML file
' Assign a value to the corresponding property
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 ' invokes the Addtoxml method of the Cls_person class, adding data
Adderr Strerr, Objperson.getlasterror
Else
Adderr Strerr, "add Success"
Response.Write ""
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"
</HEAD>
<BODY>
<% = Strerr%>
<div class= "title" Add contact information </div>
<form name= "Form1" method= "Post" action= "add.asp"
<table align= "center" width= "100%" cellspacing= "1" cellpadding= "2" border= "0" bgcolor= "#666600"
<TR bgcolor= "#ffffff" >
<TD width= "25%" bgcolor= "#e5e5e5" align= "right" <b> Name: </b> </td>
<TD width= "75%" ><input type= "text" name= "txtname" size= "" class= "input" value= "<%=checkstroutput (objperson.name)%> "> </td>
</tr>
<TR bgcolor= "#ffffff" >
<TD bgcolor= "#e5e5e5" align= "right" > <b> English name: </b> </td>
<td> <input type= "text" name= "Txtnick" size= "" class= "input" value= "<%=checkstroutput (Objperson.nick)%>" > </td>
</tr>
<TR bgcolor= "#ffffff" >
<TD bgcolor= "#e5e5e5" align= "right" > <b> Mobile: </b> </td>
<td> <input type= "text" name= "Txtmobile" size= "" class= "input" value= "<%=checkstroutput (objperson.mobile)%>" > </td>
</tr>
<TR bgcolor= "#ffffff" >
<TD bgcolor= "#e5e5e5" align= "Right" <b> Tel: </b> </td>
<td> <input type= "text" name= "Txttel" size= "" class= "input" 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= "" class= "input" 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= "" class= "input" value= "<%=checkstroutput (OBJPERSON.QQ)%>" > </td>
</tr>
<TR bgcolor= "#ffffff" >
<TD bgcolor= "#e5e5e5" align= "right" > <b> Company: </b> </td>
<td> <input type= "text" name= "Txtcompany" size= "" class= "input" value= "<%=checkstroutput (objperson.company)%>" ></ td>
</tr>
</table>
<br>
<div align= "Center"
<input type= "Submit" Name= "Btnok" value= "Submission"
<input type= "button" Name= "Btnclose" value= "Off"
</div>
</form>
</BODY>
</HTML>
<%
Set Objperson = Nothing
%>
Vii. Summary
To this, our contact information management program is done. How, how it feels, it should be quite simple. Of course, this routine has a lot to improve the place, I am here is just a good point, I hope that the reader in the Master of XML programming, modify their own perfect bar.
This routine has been tested on my own computer (Windows Server 2000, IIS5.0, and IE6.0) and on the web, and it works correctly.