no.1--build an XML database Data.xml
<?xml version= "1.0"?>
<records>
<record>
<name>caca</name>
<qq >154222225</qq>
<email>root@3ney.com</email>
</record>
<records>
no.2--Set up Objects CreateObject
Set up the Data.xml object first
Set Xmldoc=server.createobjcet ("Microsoft.XMLDOM")
xmldoc.load (Server.MapPath ("Data.xml")
no.3--selected Node Selectnode
Which node do you want to manipulate, you have to locate it, do you have a few nodes for this data.xml first?
Use a recursive function to fix:
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
With this function, you can see that this data.xml has 10 node
These node can be very simple to locate:
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 the location very simple ah, there is a way, such as positioning <name>
Xmldoc.selectsinglenode ("//name")
no.4--assign values to a node (modify the value of a node)
Learned to locate the node, using its properties, you can modify or assign a value
For example, change the value of <name> caca to Wawa
Xmldoc.selectsinglenode ("//name"). text= "Wawa"
Xmldoc.save (Server.MapPath ("Data.xml"))
no.5--Create a new node Createnewnode
with createelement or CreateNode ("", "", "")
For example: Create a new <age> under the record, just one sentence:
Xmldoc.selectsinglenode ("//record"). AppendChild (Xmldoc.createelement ("<age>"))
Give <age> Assign Value
Xmldoc.selectsinglenode ("//age"). text= "Xmldoc.save"
(Server.MapPath ("Data.xml"))
no.6--Delete a node deletenode
You have to be clear about the parent node of the node that you want to delete, and the characteristics of this node
For example: Delete <qq> node
Xmldoc.selectsinglenode ("//record"). RemoveChild (Xmldoc.selectsinglenode ("//QQ"))
For example: Delete the <name>=caca <record>
Xmldoc.selectsinglenode ("//records"). RemoveChild (Xmldoc.selectsinglenode ("//record[name= ' Caca '
)") Xmldoc.save (Server.MapPath ("Data.xml"))
The above six classic code is sure to use ASP to control the XML database to help.