An ASP version XMLDOM operation class _ Application Skills
Source: Internet
Author: User
<script language= "VBScript" runat= "Server" >
'============================================================
' Author: Be Yourself
' Time: 2005-3-15
============================================================
Class Xmlclass
Private Objxml
Private xmldoc
Private Xmlpath
'//============================================================
' <!--class initialization and logoff event-->
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
'//============================================================
' <!--create a new XML document-->
Public Function CreateNew (sname)
Set Tmpnode = objxml.createelement (sname)
Objxml.appendchild (Tmpnode)
Set CreateNew = Tmpnode
End Function
' <!--read XML documents from outside-->
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
' <!--read from the outside XML string-->
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
'//============================================================
' <!--Add a new node-->
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
' <!--add a new attribute-->
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
' <!--new node content-->
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
'========================================================================================================
' <!--gets the value of the specified attribute of the node-->
Public Function Getatt (Aname,onode)
' Aname STRING Property name
' Onode OBJECT node reference
'=============================================================
Dim tmpvalue
Tmpvalue = Onode.getattribute (aname)
Getatt = Tmpvalue
End Function
' <!--get node name-->
Public Function getnodename (onode)
' Onode OBJECT node reference
Getnodename = Onode.nodename
End Function
' <!--get the node content-->
Public Function Getnodetext (onode)
' Onode OBJECT node reference
Getnodetext = onode.childnodes (0). nodevalue
End Function
' <!--get node type-->
Public Function Getnodetype (onode)
' Onode OBJECT node reference
Getnodetype = Onode.nodevalue
End Function
' <!--all nodes with the same name in the lookup section-->
Public Function findnodes (Snode)
Dim Tmpnodes
Set tmpnodes = Objxml.getelementsbytagname (Snode)
Set findnodes = Tmpnodes
End Function
' <!--check to make a same node-->
Public Function Findnode (Snode)
Dim Tmpnode
Set Tmpnode=objxml.selectsinglenode (Snode)
Set Findnode = Tmpnode
End Function
' <!--delete a node-->
Public Function Delnode (Snode)
Dim tmpnodes,nodesss
Set Tmpnodes=objxml.selectsinglenode (Snode)
Set Nodesss=tmpnodes.parentnode
Nodesss.removechild (Tmpnodes)
End Function
' <!--replace a node-->
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
'//=============================================================================
' <!--save XML document-->
Public Function SaveXML ()
' ProcessingInstruction ()
Objxml.save (Xmlpath)
End Function
' <!--save XML document-->
Public Function Saveasxml (spath)
ProcessingInstruction ()
Objxml.save (spath)
End Function
'//==================================================================================
' Related statistics
' <!--get root node-->
Property Get Root
Set Root = xmldoc
End Property
' <!--get the number of nodes in the root node-->
Property Get Length
Length = XmlDoc.childNodes.length
End Property
'//==================================================================================
' Related tests
Property Get Testnode
Testnode = xmldoc.childnodes (0). Text
End Property
End Class
</script>
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