ASP XML Operation Class Code _ Application skills

Source: Internet
Author: User
Tags error handling
Copy Code code as follows:
Class Xmlclass
Private Objxml
Private xmldoc
Private Xmlpath
'//============================================================
'
Sub Class_Initialize
Set objxml = Server.CreateObject ("MSXML2. DOMDocument ")
Objxml.preservewhitespace = True
Objxml.async = False
End Sub
Sub Class_Terminate
Set Objxml = Nothing
End Sub
'//============================================================
'
Public Function CreateNew (sname)
Set Tmpnode = objxml.createelement (sname)
Objxml.appendchild (Tmpnode)
Set CreateNew = Tmpnode
End Function
'
Public Function OpenXml (spath)
Openxml=false
Spath=server.mappath (spath)
' Response.Write (spath)
Xmlpath = spath
If objxml.load (spath) Then
Set xmldoc = objxml.documentelement
Openxml=true
End If
End Function
'
Public Sub Loadxml (SSTR)
Objxml.loadxml (SSTR)
Set xmldoc = objxml.documentelement
End Sub
Public Sub Inceptxml (xobj)
Set Objxml = Xobj
Set xmldoc = xobj.documentelement
End Sub
'//============================================================
'
Public Function AddNode (Snode,rnode)
' Snode STRING node name
' Rnode OBJECT increases the ancestor node reference of the node
'=============================================================
Dim Tmpnode
Set Tmpnode = objxml.createelement (Snode)
Rnode.appendchild Tmpnode
Set AddNode = Tmpnode
End Function
'
Public Function AddAttribute (Sname,svalue,onode)
' Sname STRING Property name
' Svalue STRING Property value
' Onode object to increase property
'=============================================================
Onode.setattribute Sname,svalue
End Function
'
Public Function addtext (Fstr,cdbool,onode)
Dim Tmptext
If Cdbool Then
Set Tmptext = objxml.createcdatasection (FSTR)
Else
Set Tmptext = Objxml.createtextnode (FSTR)
End If
Onode.appendchild Tmptext
End Function
'========================================================================================================
'
Public Function Getatt (Aname,onode)
' Aname STRING Property name
' Onode OBJECT node reference
'=============================================================
Dim tmpvalue
Tmpvalue = Onode.getattribute (aname)
Getatt = Tmpvalue
End Function
'
Public Function getnodename (onode)
' Onode OBJECT node reference
Getnodename = Onode.nodename
End Function
'
Public Function Getnodetext (onode)
' Onode OBJECT node reference
Getnodetext = onode.childnodes (0). nodevalue
End Function
'
Public Function Getnodetype (onode)
' Onode OBJECT node reference
Getnodetype = Onode.nodevalue
End Function
'
Public Function findnodes (Snode)
Dim Tmpnodes
Set tmpnodes = Objxml.getelementsbytagname (Snode)
Set findnodes = Tmpnodes
End Function
'
Public Function Findnode (Snode)
Dim Tmpnode
Set Tmpnode=objxml.selectsinglenode (Snode)
Set Findnode = Tmpnode
End Function
'
Public Function Delnode (Snode)
Dim tmpnodes,nodesss
Set Tmpnodes=objxml.selectsinglenode (Snode)
Set Nodesss=tmpnodes.parentnode
Nodesss.removechild (Tmpnodes)
End Function
'
Public Function ReplaceNode (Snode,stext,cdbool)
' ReplaceChild
Dim Tmpnodes,tmptext
Set Tmpnodes=objxml.selectsinglenode (Snode)
' AddText stext,cdbool,tmpnodes
If Cdbool Then
Set Tmptext = objxml.createcdatasection (stext)
Else
Set Tmptext = Objxml.createtextnode (stext)
End If
Tmpnodes.replacechild Tmptext,tmpnodes.firstchild
End Function

Private Function ProcessingInstruction
'//--create XML declaration
Dim OBJPI
Set OBJPI = objxml.createprocessinginstruction ("xml", "Version=" &CHR () & "1.0" &CHR (%) & "encoding=" &CHR (& "gb2312" &CHR (34))
'//--to append XML life to an XML document
Objxml.insertbefore Objpi, objxml.childnodes (0)
End Function
'//=============================================================================
'
Public Function SaveXML ()
' ProcessingInstruction ()
Objxml.save (Xmlpath)
End Function
'
Public Function Saveasxml (spath)
ProcessingInstruction ()
Objxml.save (spath)
End Function
'//==================================================================================
' Related statistics
'
Property Get Root
Set Root = xmldoc
End Property
'
Property Get Length
Length = XmlDoc.childNodes.length
End Property
'//==================================================================================
' Related tests
Property Get Testnode
Testnode = xmldoc.childnodes (0). Text
End Property
End Class


The main method and implementation of ASP to manipulate XML file on server side by XMLDOM
For small amounts of data, XML files have many advantages in retrieving updates on access.

I have tested the use of the database, the site's membership information, merchandise data information, transaction information, Web site customization information stored in three XML files, the results of the operation is very normal, feeling more than the database faster, but did not test, can not be determined.

Here are the main ways to create, query, modify, and so on XML operations

' Create a DOM object
Set Objdom=server. CreateObject ("Microsoft.XMLDOM")

' Get XML data
' Method 1 Gets the XML data of the XML file
Objdom.load ("C:\test.xml")
' Method 2 Gets the data of the XML data string
Objdom.loadxml ("<people><man name=" SD "/></people>")

' Create a Node object
Set newnode=objdom.createelement ("people")
' To the value of this node
Newnode.text= "Man"
' Add attributes to this node
Set newattribute=objdom.createnode ("attribute", "name", "")
newattribute.text= "John"
Newnode.setattributenode Newattribute
' Add child nodes to this node
Set newnodechild=objdom.createelement ("address")
Newnode.appendchild Newnodechild
' Save this Node object
Objdom.appendchild Newnode
Objdom.save ("C:\test.xml")

' Find a Node object
Set Objtofind=objdom.documentelement.selectsinglenode ("//people/man")
' Take out the node name of this node object, the node value, a property value, and all the XML
Nodename=objtofind.nodename
Nodevalue=objtofind.text
Objtofind. GetAttributeNode ("name"). NodeValue ' property value named Name

' Remove an Attribute node object
Set Objattrtofind=objdom.documentelement.selectsinglenode ("//people/man"). GetAttributeNode ("name")
' Take out the attribute name of this node, attribute value
Nodeattrname=objattrtofind.nodename
Nodeattrvalue=objattrtofind.nodevalue

' Delete a Node object
Set Objnode=objdom.documentelement.selectsinglenode ("//people/man") ' the node to be deleted
Set Objparentnode=objdom.documentelement.selectsinglenode ("//people") ' parent node of the node to be deleted
Objparentnode.removechild Objnode

' Take out a set of byte points for a node
Set Objnodes=objdom.documentelement.selectsinglenode ("//people/man"). ChildNodes
Traverse this collection
Method 1
For each element in Objnodes
Response.Write Element.nodename Byte Roll Call
Response.Write Element.text Byte point value
Next
Method 2
Domlength=objnodes.length
For i = 0 to Domlength-1
Response.Write Objnodes.childnodes (i)-nodename byte Roll Call
Response.Write Objnodes.childnodes (i). Text byte point value
Next

' Remove a collection of attributes from a node
Set Objnodes=objdom.documentelement.selectsinglenode ("//people/man"). GetAttributeNode ("name"). Attributes
Traverse this collection
For each element in Objnodes
Response.Write Element.nodename Property Name
Response.Write Element.nodevalue Property Value
Next

And so can skillfully use the XMLDOM object to manipulate the XML file, you can enjoy
By the XMLHTTP object to implement many of the functions of ASP.

re:[Transfer]asp The main method and implementation of XML file operation via XMLDOM on server side
Fei Fei, why don't you introduce the use of @, without this, a lot of things inconvenient.
XML is also known as data compression technology, as the name implies, XML can act as a database to use.
So, we can think of XML as a "small database". Why do you say it's small? Because the function and application of XML itself is convenient, there is a certain difference with the database. So why do we use XML? Because, sometimes some of our applications, although data access, but, if the use of the database, the explicit line is somewhat inflexible and convenient. At this point, we should use XML in conjunction with it.
Since XML can be viewed as a database, the first step in its work is of course to create linked objects. (Take Asp+xml as an example)
Create a method, like a linked database, with Server.CreateObject.
The method is as follows:
Set xmldoc= Server.CreateObject ("Microsoft.XMLDOM")
Xmldoc.async=false
Xmldata= Data Source Absolute path
Xmldoc.load XMLData ' Here using the Load method for linking

Both in the XML data format is more humane, can be caused by artificial or other reasons, the data format is not legitimate, then if you continue to use, will cause the program, so we often create a good link object after the data format verification.
The method is as follows:
If Xmldoc.parseerror.errorcode<>0 Then
.... Error handling


<%
‘—————————————————————-
' Introduction: Complete the ASP language to add, delete, modify, view the text of the specified node in the XML document
' Entry parameters: None
' Exit Parameters: None
‘————————————————
' Function name: Connectxml ()
' Entry parameter: FileName required to be connected or opened by XML filename
' Exit Parameters: None
' Return value: Connectxml=0,xmlmorntekdocument is an object that successfully loads an XML document.
' Connectxml<>0, the error message is printed strerror
‘————————————————
Dim xmlmorntekdocument

function connectxml (filename)
Dim strsourcefile
strSourceFile = Server.MapPath (filename)
Set xmlmorntekdocument = Server.CreateObject ("Microsoft.XMLDOM")
Xmlmorntekdocument.async = False
Xmlmorntekdocument.load (strSourceFile)
Connectxml=xmlmorntekdocument.parseerror.errorcode
If Xmlmorntekdocument.parseerror.errorcode<>0 Then
Strerror= "strerror=strerror&xmlmorntekdocument.parseerror.reason& "<br>"
strerror=strerror&xmlmorntekdocument.parseerror.url& "<br>"
strerror=strerror&xmlmorntekdocument.parseerror.line& "<br>"
strerror=strerror&xmlmorntekdocument.parseerror.filepos& "<br>"
strerror=strerror&xmlmorntekdocument.parseerror.srctext& "<br>"
Response.Write strerror
End If
End Function

‘————————————————
' Function name: Closexml ()
' Entry parameters: None
' Exit Parameters: None
‘————————————————
function Closexml (xmlmorntekdocument)
If IsObject (xmlmorntekdocument) Then
Set xmlmorntekdocument=nothing
End If
End Function

‘————————————————
' Function name: Selectxmlnodetext (elementname)
' Entry parameter: The name of the elementname element
' Exit Parameters: None
‘————————————————
function Selectxmlnodetext (elementname)
Elementname= "//" &elementname
Temp=xmlmorntekdocument.selectsinglenode (elementname). Text
selectxmlnodetext= Server.HTMLEncode (temp)

End Function

‘————————————————
' Function name: Insertxmlnodetext (Befelementname,elementname,elementtext)
' Entry parameter: ElementName the name of the inserted element
' Befelementname inserts an element before the name of this element
' Elementtext the text of the inserted element
' Exit Parameters: None
‘————————————————
function Insertxmlnodetext (befelementname,elementname,elementtext)
Dim befelement,element
Set Befelement=xmlmorntekdocument.selectsinglenode ("//" &befelementname)
Set element= xmlmorntekdocument.createelement (elementname)
Befelement.insertbefore Element,befelement.firstchild
Element.text=elementtext
End Function

‘————————————————
' Function name: Updatexmlnodetext (Elementname,newelementtext)
' Entry parameter: The name of the elementname element
' New text for newelementtext element
' Exit Parameters: None
‘————————————————
function Updatexmlnodetext (elementname,newelementtext)
Dim element
Set Element=xmlmorntekdocument.selectsinglenode ("//" &elementname)
Element.text=newelementtext
End Function

‘————————————————
' Function name: Deletexmlnodetext (elementname)
' Entry parameter: The name of the elementname element
' Exit Parameters: None
‘————————————————
function Deletexmlnodetext (elementname)
Xmlmorntekdocument.selectsinglenode ("//" &elementname). Text = ""
End Function
%>
____________________

This article is not tested, the feasibility of the article is unknown.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.