An ASP xmldom operation class

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 logout events -->
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 the external -->
Public Function openxml (Spath)
Openxml = false
Spath = server. mappath (Spath)
'Response. Write (Spath)
Xmlpath = Spath
If objxml. Load (Spath) then
Set xmldoc = objxml.doc umentelement
Openxml = true
End if
End Function
'<! -- Read XML strings from the external -->
Public sub loadxml (SSTR)
Objxml. loadxml (SSTR)
Set xmldoc = objxml.doc umentelement
End sub
Public sub inceptxml (xobj)
Set objxml = xobj
Set xmldoc = xobj.doc umentelement
End sub
'// ============================================== ======================================
'<! -- Add a node -->
Public Function addnode (snode, rnode)
'Snode string node name
'Rnode object adds a parent node reference for the node
'================================================ ======================================
Dim tmpnode
Set tmpnode = objxml. createelement (snode)
Rnode. appendchild tmpnode
Set addnode = tmpnode
End Function
'<! -- Add an attribute -->
Public Function addattride (sname, svalue, onode)
'Sname string property name
'Svalue string property value
'Onode object: Add attribute objects
'================================================ ======================================
Onode. setattribute sname, svalue
End Function
'<! -- Add 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
'================================================ ========================================================== ======================================
'<! -- Get the value of the specified node attribute -->
Public Function getatt (aname, onode)
'Aname string property name
'Onode object node reference
'================================================ ======================================
Dim tmpvalue
Tmpvalue = onode. getattribute (aname)
Getatt = tmpvalue
End Function
'<! -- Get the node name -->
Public Function getnodename (onode)
'Onode object node reference
Getnodename = onode. nodename
End Function
'<! -- Get node content -->
Public Function getnodetext (onode)
'Onode object node reference
Getnodetext = onode. childnodes (0). nodevalue
End Function
'<! -- Obtain the node type -->
Public Function getnodetype (onode)
'Onode object node reference
Getnodetype = onode. nodevalue
End Function
'<! -- Find all nodes with the same node name -->
Public Function findnodes (snode)
Dim tmpnodes
Set tmpnodes = objxml. getelementsbytagname (snode)
Set findnodes = tmpnodes
End Function
'<! -- Create an identical 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)
'Replace child
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 an XML Declaration
Dim objpi
Set objpi = objxml. createprocessinginstruction ("XML", "version =" & CHR (34) & "1.0" & CHR (34) & "encoding =" & CHR (34) & "gb2312" & CHR (34 ))
'// -- Append the XML life to the XML document
Objxml. insertbefore objpi, objxml. childnodes (0)
End Function
'// ============================================== ==========================================================
'<! -- Save the XML document -->
Public Function savexml ()
'Processinginstruction ()
Objxml. Save (xmlpath)
End Function
'<! -- Save the XML document another -->
Public Function saveasxml (Spath)
Processinginstruction ()
Objxml. Save (Spath)
End Function
'// ============================================== ========================================================== =====
'Related statistics
'<! -- Get the root node -->
Property get root
Set root = xmldoc
End Property
'<! -- Get the number of subnodes under 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>

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.