ASP Method for XML operations

Source: Internet
Author: User
Tags html encode

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? Sometimes some of our applications perform data access, but if you use a database, the explicit behavior is not flexible and convenient. 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 = "<H2> error" & xmlmorntekdocument. parseerror. errorcode & "</H2>"
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 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
%>

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.