ASP xmldom The Main method of operating XML file on server side and implementation _ Application skill

Source: Internet
Author: User
For small amounts of data, XML files have many advantages in retrieving updates on access.

I have tested the use of the database, the site's membership information, merchandise data information, transaction information, Web site customization information stored in three XML files, the results of the operation is very normal, feeling more than the database faster, but did not test, can not be determined.

Here are the main ways to create, query, modify, and so on XML operations

Program code

no.1--build an XML database Data.xml
Copy Code code as follows:

<?xml version= "1.0"?>

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:
Copy 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>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))
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" (Server.MapPath ("Data.xml"))
no.5--Create a new node Createnewnode
With createelement or CreateNode ("", "", "")
For example: Create a new <age&gt under the record, just one sentence:
Xmldoc.selectsinglenode ("//record"). AppendChild (Xmldoc.createelement ("<age>"))
Give <age> Assign Value
Xmldoc.selectsinglenode ("//age"). text= "20" (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 ')") (Server.MapPath ("Data.xml"))
Only can skilled these 6 code, uses the ASP to control the XML database, also is almost ...

' Create a DOM object
Set Objdom=server. CreateObject ("Microsoft.XMLDOM")

' Get XML data
' Method 1 Gets the XML data of the XML file
Objdom.load ("C:\test.xml")
' Method 2 Gets the data of the XML data string
Objdom.loadxml ("<people><man name=" SD "/></people>")

' Create a Node object
Set newnode=objdom.createelement ("people")
' To the value of this node
Newnode.text= "Man"
' Add attributes to this node
Set newattribute=objdom.createnode ("attribute", "name", "")
newattribute.text= "John"
Newnode.setattributenode Newattribute
' Add child nodes to this node
Set newnodechild=objdom.createelement ("address")
Newnode.appendchild Newnodechild
' Save this Node object
Objdom.appendchild Newnode ("C:\test.xml")

' Find a Node object
Set Objtofind=objdom.documentelement.selectsinglenode ("//people/man")
' Take out the node name of this node object, the node value, a property value, and all the XML
Objtofind. GetAttributeNode ("name"). NodeValue ' property value named Name

' Remove an Attribute node object
Set Objattrtofind=objdom.documentelement.selectsinglenode ("//people/man"). GetAttributeNode ("name")
' Take out the attribute name of this node, attribute value

' Delete a Node object
Set Objnode=objdom.documentelement.selectsinglenode ("//people/man") ' the node to be deleted
Set Objparentnode=objdom.documentelement.selectsinglenode ("//people") ' parent node of the node to be deleted
Objparentnode.removechild Objnode

' Take out a set of byte points for a node
Set Objnodes=objdom.documentelement.selectsinglenode ("//people/man"). ChildNodes
Traverse this collection
Method 1
For each element in Objnodes
Response.Write Element.nodename Byte Roll Call
Response.Write Element.text Byte point value
Method 2
For i = 0 to Domlength-1
Response.Write Objnodes.childnodes (i)-nodename byte Roll Call
Response.Write Objnodes.childnodes (i). Text byte point value

' Remove a collection of attributes from a node
Set Objnodes=objdom.documentelement.selectsinglenode ("//people/man"). GetAttributeNode ("name"). Attributes
Traverse this collection
For each element in Objnodes
Response.Write Element.nodename Property Name
Response.Write Element.nodevalue Property Value

Can skillfully use the XMLDOM object to manipulate the XML file, you can enjoy the XMLHTTP object to achieve many of the functions of ASP.


Although ASP can only operate XML files simply, it is sufficient for general program developers.
Before, the XML language very little contact, later slowly, found that XML in the storage of data has a lot of convenience. Although the security is not good (personally think), but for the general data storage is indeed a very good choice.
Today, because XML is needed on a Web site, I'm going to do some summary here (later on):
First, the ASP read XML file First
Default.asp's 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")
Section <%=i%> Record:
<table width= "50%" border= "1" >
&LT;TD width= "rowspan=" 2 "> "/></td>"
&LT;TD width= > Title </td>
&LT;TD width= "> Press" </td>
&LT;TD width= > Price </td>
<%=node.selectsinglenode ("name") .text%>
<%=node.selectsinglenode ("publisher") .text%>
<%= cost.text%>
Next up is the Data.xml data content
<?xml version= "1.0" encoding= "UTF-8"?>
<book cost= ">"
<publisher> China Railway Publishing house </publisher>
<book cost= ">"
<publisher> China Railway Publishing house </publisher>
<book cost= ">"
<publisher> China Railway Publishing house </publisher>
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: 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.