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.
ProgramCode
No.1 -- create an XML database data. xmlCopy codeThe Code is as follows: <? XML version = "1.0"?>
<Records>
<Record>
<Name> caca </Name>
<QQ> 154222225 </QQ>
<Email> root@3ney.com </Email>
</Record>
<Records>
No. 2 -- create object Createobject
Create a data. XML Object first
Set xmldoc = server. createobjcet ("Microsoft. xmldom ")
Xmldoc. Load (server. mappath ("data. xml ")
No. 3 -- selectnode
Which node do you want to operate? Do you have to locate this node? check whether there are several nodes in this data. xml file first ??
Use a recursive function:Copy codeThe Code is as follows: getnodes (xmldoc)
Sub getnodes (node)
Dim I
Response. write ("<br> <B> nodename: </B>" & node. nodename & "<br> <B> nodetypestring: </B>" & node. nodetypestring & "<br> <B> nodevalue: </B>" & node. nodevalue & "<br> <B> text: </B>" & node. text & "<br> <B> node. childnodes. length: </B> "& node. childnodes. length & "<p> ")
If node. childnodes. length <> 0 then
for I = 0 to node. childnodes. length-1
getnodes (node. childnodes (I)
next
end if
end sub
after using this function, you can see this data. XML has 10 nodes
these nodes can be easily located:
xmldoc. childnodes (0)
xmldoc. childnodes (1)
xmldoc. childnodes (1 ). childnodes (0)
xmldoc. childnodes (1 ). childnodes (0 ). childnodes (0)
xmldoc. childnodes (1 ). childnodes (0 ). childnodes (0 ). text
xmldoc. childnodes (1 ). childnodes (0 ). childnodes (1)
xmldoc. childnodes (1 ). childnodes (0 ). childnodes (1 ). text
xmldoc. childnodes (1 ). childnodes (0 ). childnodes (2)
xmldoc. childnodes (1 ). childnodes (0 ). childnodes (2 ). text
isn't positioning very easy? There is another method, such as locating
xmldoc. selectsinglenode ("// name")
No. 4 -- assign a value to a node (modify the value of a node)
after learning to locate a node, you can modify or assign a value using its attributes.
for example, change the value of caca to Wawa
xmldoc. selectsinglenode ("// name "). TEXT = "Wawa"
xmldoc. save (server. mappath ("data. XML ")
Get it done!
No. 5 -- create a new node createnewnode
Use createelement or createnode ("", "", "")
example: create a new under record. You only need to use one sentence:
xmldoc. selectsinglenode ("// record "). appendchild (xmldoc. createelement (" ")
assign a value to
xmldoc. selectsinglenode ("// age "). TEXT = "20"
xmldoc. save (server. mappath ("data. XML ")
Get it done!
No. 6 -- delete a node deletenode
you must specify the parent node of the node you want to delete and its features.
example: delete node
xmldoc. selectsinglenode ("// record "). removechild (xmldoc. selectsinglenode ("// QQ")
for example, delete the = caca
xmldoc. selectsinglenode ("// records "). removechild (xmldoc. selectsinglenode ("// record [name = 'caca'])
xmldoc. save (server. mappath ("data. XML ")
Get it done!
only these 6 codes can be mastered. Using ASP to control the XML database is almost the same...
================================================= ================================
'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
'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
If You Can skillfully use xmldom objects to operate XML files, you can enjoy XMLHTTP objects to implement many functions under ASP.
Bytes ------------------------------------------------------------------------------------------
Although ASP can only operate XML files, it is sufficient for general program developers.
In the past, the XML language was rarely used. Later, I found that XML has a lot of convenience in data storage. Although the security is poor (I personally think), it is indeed a very good choice for general data storage.
Today, Because XML is used on a website, I will make some summary here (which will be used later ):
====================
First, sort out ASP to read XML files.
Default. asp code
<%
Dim node, I, nodecount
Set Doc = Createobject ("Microsoft. xmldom ")
Doc. async = false
Doc. Load (server. mappath ("data. xml "))
Set root = doc.doc umentelement
Set nodelis = root. childnodes
Nodecount = nodelis. Length
For I = 1 to nodecount
Set node = nodelis. nextnode ()
Set cost = node. Attributes. getnameditem ("cost ")
%>
Record <% = I %>:
<Table width = "50%" border = "1">
<Tr>
<TD width = "43" rowspan = "2"> </TD>
<TD width = "46"> title </TD>
<TD width = "48"> press </TD>
<TD width = "42"> price </TD>
</Tr>
<Tr>
<TD>
<% = Node. selectsinglenode ("name"). Text %>
</TD>
<TD>
<% = Node. selectsinglenode ("publisher"). Text %>
</TD>
<TD>
<% = Cost. Text %>
</TD>
</Tr>
</Table>
<%
Next
%>
================================
Next is the data. XML data content.
<? XML version = "1.0" encoding = "UTF-8"?>
<DATA>
<Book cost = "56">
<Name> Dreamweaver </Name>
<Publisher> China Railway Publishing House </publisher>
img/dw.jpg </img>
</Book>
<Book cost = "62">
<Name> flash </Name>
<Publisher> China Railway Publishing House </publisher>
img/flash.jpg </img>
</Book>
<Book cost = "48">
<Name> firweorks </Name>
<Publisher> China Railway Publishing House </publisher>
img/fw.jpg </img>
</Book>
</Data>