ASP control the classical code of XML database application techniques

Source: Internet
Author: User

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.

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.