I once tested without using a database and stored the website's member information, product data information, transaction information, and website customization information in three xml files. The running results were very normal. It felt much faster than the database, but I didn't test it. Not sure. For small data volumes, xml files have many advantages over ACCESS in retrieval and update.
I once tested without using a database and stored the website's member information, product data information, transaction information, and website customization information in three xml files. The running results were very normal. It felt much faster than the database, but I didn't test it. Not sure.
Let’s talk about the main methods of creating, querying, modifying, etc. xml operations
program code
NO.1--Create an XML database data.xml
Copy the code code as follows:
<?xml version=1.0?>
<records>
<record>
<name>caca</name>
<qq>154222225</qq>
<email>[email protected]</email>
</record>
<records>
NO.2--Create objectCreateObject
Create the object of data.xml first
set xmldoc=server.createobjcet(microsoft.xmldom)
xmldoc.load(server.mappath(data.xml)
NO.3--Selected nodeSelectNode
Which Node do you want to operate? You must locate this node, right? First look at how many Nodes this data.xml has??
Do it with a recursive function:
Copy the code code 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>NodeVal ue:</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 that this data.xml has 10 Nodes.
These Nodes can be easily located:
xmldoc
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
Is positioning very simple? There is another way, such as positioning <name>
xmldoc.selectsinglenode(//name)
NO.4--Assign value to node (modify the value of node)
Once you learn to locate nodes and use their attributes, you can modify or assign values.
For example, change the value of <name> from caca to wawa
xmldoc.selectsinglenode(//name).text=wawa
xmldoc.save(server.mappath(data.xml))
Done!
NO.5--Create a new nodeCreatenewNode
Use createelement or createnode(,,)
For example: Create a new <age> under record, and it only takes one sentence:
xmldoc.selectsinglenode(//record).appendchild(xmldoc.createelement(<age>))
Assign a value to <age>
xmldoc.selectsinglenode(//age).text=20
xmldoc.save(server.mappath(data.xml))
Done!
NO.6--Delete a nodeDeleteNode
You must specify the parent node of the node you want to delete, as well as the characteristics of this node
For example: delete <qq> node
xmldoc.selectsinglenode(//record).removechild(xmldoc.selectsinglenode(//qq))
For example: delete the <record> with <name>=caca
xmldoc.selectsinglenode(//records).removechild(xmldoc.selectsinglenode(//record[name='caca']))
xmldoc.save(server.mappath(data.xml))
Done!
As long as you are proficient in these 6 codes and use asp to control the xml database, that's almost it...
================================================== ======
'Create DOM object
set objDom=server.CreateObject(MicroSoft.XMLDom)
'Get xml data
'Method 1 Get the xml data of the xml file
objDom.load(c:/test.xml)
'Method 2 Get the data of xml data string
objDom.loadxml(<people><man name=sd/></people>)
'Create a node object
Set Newnode=objDom.CreateElement(people)
'Give this node a value
Newnode.Text=People
'Add attributes to this node
Set NewAttribute=objDom.CreateNode(attribute,name,)
NewAttribute.Text= Zhang San
Newnode.SetAttributeNode NewAttribute
'Add child nodes to this node
Set NewnodeChild=objDom.CreateElement(address)
Newnode.appendChild NewnodeChild
'Save this node object
objDom.appendChild Newnode
objDom.save(c:/test.xml)
'Find a node object
set objtofind=objdom.documentElement.SelectSingleNode(//people/man)
'Get the node name, node value, a certain attribute value, and all xml of this node object
nodename=objtofind.nodename
nodevalue=objtofind.text
objtofind.GetAttributeNode(name).NodeValue 'The attribute value of the attribute named name
'Get an attribute node object
set objattrtofind=objdom.documentElement.SelectSingleNode(//people/man).GetAttributeNode(name)
'Get the attribute name and attribute value of this node
nodeattrname=objattrtofind.nodename
nodeattrvalue=objattrtofind.nodevalue
'Delete a node object
set objnode=objdom.documentElement.SelectSingleNode(//people/man) 'The node to be deleted
set objparentnode=objdom.documentElement.SelectSingleNode(//people) 'The parent node of the node to be deleted
objparentnode.removeChild objnode
'Get the byte point set of a node
set objnodes=objdom.documentElement.SelectSingleNode(//people/man).ChildNodes
Iterate over this collection
Method 1
for each element in objnodes
response.write element.nodename byte naming
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 naming
response.write objnodes.childnodes(i).text byte point value
next
'Get the attribute set of a node
set objnodes=objdom.documentElement.SelectSingleNode(//people/man).GetAttributeNode(name).attributes
Iterate over this collection
for each element in objnodes
response.write element.nodename attribute name
response.write element.nodevalue attribute value
next
Once you are able to skillfully use the xmldom object to operate xml files, you can enjoy the xmlhttp object to implement many functions under asp.
-------------------------------------------------- ----------------------------------------
Although ASP can only simply operate XML files, it is enough for general program developers.
Before, I had very little exposure to the XML language. Later, I gradually discovered that XML has a lot of conveniences in storing data. Although the security is not good (in my opinion), it is indeed a very good choice for general data storage.
Today, because XML needs to be used on a website, I will make some summaries here (will be used later):
==============
First, organize ASP to read the XML file
default.asp code
<%
dim node,i,nodecount
set Doc = CreateObject(Microsoft.XMLDOM)
Doc.async = false
Doc.load(Server.MapPath(data.xml))
set root = Doc.documentElement
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><img src=<%=node.selectSingleNode(img).text%>/></td>
<td width=46>Book title</td>
<td width=48>Publishing House</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 content of Data.xml
<?xml version=1.0 encoding=UTF-8?>
<data>
<book cost=56>
<name>Dreamweaver</name>
<publisher>China Railway Press</publisher>
<img>img/dw.jpg</img>
</book>
<book cost=62>
<name>Flash</name>
<publisher>China Railway Press</publisher>
<img>img/flash.jpg</img>
</book>
<book cost=48>
<name>Firweorks</name>
<publisher>China Railway Press</publisher>
<img>img/fw.jpg</img>
</book>
</data>