Asp xml operation code

Source: Internet
Author: User
Tags html encode

CopyCode The Code is 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.doc umentelement
Openxml = true
End if
End Function
'
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
'// ============================================== ======================================
'
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
'
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
'
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)
'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
'// ============================================== ==========================================================
'
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 operating XML files on the server through xmldom
For small data volumes, XML files have many advantages over access in retrieval and update.

I have tested whether to use a database and store all the Website member information, product data information, transaction information, and website customization information in three XML files. The running result is normal, it seems that it is much faster than the database, but it is not tested and cannot be determined.

The following describes how to create, query, modify, and perform XML operations.

'Create a DOM object
Set objdom = server. Createobject ("Microsoft. xmldom ")

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

'Create a Node object
Set newnode = objdom. createelement ("people ")
'Return value to this node
Newnode. Text = "person"
'Add attributes to this node
Set newattride = objdom. createnode ("attribute", "name ","")
Newattri. Text = "James"
Newnode. setattributenode newattribute
'Add a subnode to this node
Set newnodechild = objdom. createelement ("Address ")
Newnode. appendchild newnodechild
'Save this Node object
Objdom. appendchild newnode
Objdom. Save ("C: \ test. xml ")

'Look up a Node object
Set objtofind=objdom.doc umentelement. selectsinglenode ("// people/Man ")
'Get the node name, node value, attribute value, and all XML of the Node object.
Nodename = objtofind. nodename
Nodevalue = objtofind. Text
Objtofind. getattributenode ("name"). nodevalue' attribute name: attribute value of name

'Retrieve an attribute Node object
Set objattrtofind=objdom.doc umentelement. selectsinglenode ("// people/Man"). getattributenode ("name ")
'Retrieve the attribute name and attribute value of this node.
Nodeattrname = objattrtofind. nodename
Nodeattrvalue = objattrtofind. nodevalue

'Delete a Node object
Set objnode1_objdom.doc umentelement. selectsinglenode ("// people/man") 'node to be deleted
Set objparentnode1_objdom.doc umentelement. selectsinglenode ("// people") 'parent node of the node to be deleted
Objparentnode. removechild objnode

'Retrieve the byte point set of a node
Set objnodesnodes objdom.doc umentelement. selectsinglenode ("// people/man"). childnodes
Traverse this set
Method 1
For each element in objnodes
Response. Write element. nodename byte name
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 name
Response. Write objnodes. childnodes (I). Text byte point value
Next

'Retrieve the attribute set of a node
Set objnodesincluobjdom.doc umentelement. selectsinglenode ("// people/man"). getattributenode ("name"). Attributes
Traverse this set
For each element in objnodes
Response. Write element. nodename attribute name
Response. Write element. nodevalue Attribute Value
Next

You can use xmldom objects to operate XML files.
Many ASP functions are implemented by XMLHTTP objects.

Re: [to] ASP main methods and implementation of XML file operations on the server through xmldom
Feifei, why don't you describe the @ usage? Without this, it is inconvenient to do a lot of things.
XML, also known as data compression technology, can be used as a database as its name suggests.
Therefore, we can regard XML as a "small database ". Why is it small? Because the functions and applications of XML are convenient, there are still some differences with databases. Why should we use XML? Because sometimes some of our applicationsProgramAlthough data is accessed, the explicit display is not flexible and convenient if the database is used. At this time, we should use XML in combination.
Since XML can be viewed as a database, its first step is to create a link object. (Take ASP + XML as an example)
Use server. Createobject to create a database.
The method is as follows:
Set xmldoc = server. Createobject ("Microsoft. xmldom ")
Xmldoc. async = false
Xmldata = absolute path of the Data Source
Xmldoc. Load xmldata 'use the load method for link here

Because the XML data format is more user-friendly, the data format may be invalid due to human or other reasons. In this case, if you continue to use it, it will cause the program to exit, we often verify the data format after creating a link object.
The method is as follows:
If xmldoc. parseerror. errorcode <> 0 then
.... Error Handling

<%
'----------------------
'Program Introduction: added, deleted, modified, and viewed the specified node text in the XML document in ASP.
'Entry parameter: None
'Exit parameter: None
'----------------
'Function name: connectxml ()
'Entry parameter: the XML file name to be connected or opened by filename
'Exit parameter: None
'Return value: connectxml = 0. xmlmorntekdocument is an object that successfully loads the XML document.
'Connectxml <> 0, the error message strerror is printed.
'----------------
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 = "

error" & xmlmorntekdocument. parseerror. errorcode & "

"
strerror = strerror & xmlmorntekdocument. parseerror. reason & "
strerror = strerror & xmlmorntekdocument. parseerror. URL & "
strerror = strerror & xmlmorntekdocument. parseerror. line & "
strerror = strerror & xmlmorntekdocument. parseerror. filepos & "
strerror = strerror & xmlmorntekdocument. parseerror. srctext & "
response. write strerror
end if
end function

'----------------
'Function name: closexml ()
'Entry parameter: None
'Exit parameter: 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 parameter: None
'----------------
Function selectxmlnodetext (elementname)
Elementname = "//" & elementname
Temp = xmlmorntekdocument. selectsinglenode (elementname). Text
Selectxmlnodetext = server.html encode (temp)

End Function

'----------------
'Function name: insertxmlnodetext (befelementname, elementname, elementtext)
'Entry parameter: the name of the element inserted by elementname
'Befelementname: insert an element before the element name
'Elementtext: Text of the inserted element
'Exit parameter: 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
'Newelementtext
'Exit parameter: 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 parameter: None
'----------------
Function deletexmlnodetext (elementname)
Xmlmorntekdocument. selectsinglenode ("//" & elementname). Text = ""
End Function
%>
____________________

This article has not been tested,ArticleThe feasibility 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.