Books. asp
<%
''Create an XML Object
Set objxmldom = server. Createobject ("Microsoft. xmldom ")
''Specifies whether the XML file download is asynchronous with the XML Processing
Objxmldom. async = false
''To get the Directory and file name of the XML file. Here is the virtual directory.
Filestr = server. mappath ("./") & "/books. xml"
''Load the XML file
Objxmldom. Load (filestr)
''To get the length of the subnode
''Nodeslength = objxmldom. documentelement. childnodes. Length
''Response. Write (nodeslength & "<br> ")
'Get the parent node object
Set parentnodes = objxmldom. documentelement
'Get the length of the subnode
Childnodeslength = parentnodes. childnodes. Length
'Length of the output subnode
Response. Write ("Number of subnodes:" & parentnodes. childnodes. length)
Response. Write ("<br> ")
'Traverse the attributes and nodes of the subnode
For I = 0 to childNodesLength-1
Set objchile = parentnodes. childnodes (I)
Response. Write (I + 1 & ": bookid =" & objchile. getattribute ("ID "))
Response. Write (", sortid =" & objchile. getattribute ("sortid "))
Response. Write (", Title =" & objchile. selectsinglenode ("title"). Text)
Response. Write (", author =" & objchile. selectsinglenode ("author"). Text & "<br> ")
Next
''= Add a subnode
'Get an object of the root node.
Set objsortlist = objxmldom.doc umentelement. selectsinglenode ("// books ")
''Determines whether a subnode exists,
If objsortlist. haschildnodes then
''If a subnode exists, the value of ID in the last subnode is obtained, and 1 is added as the id value of the new subnode.
Id = objsortlist. lastchild. getattribute ("ID") + 1
'Different values are obtained based on the structure of the XML file.
''Id = objsortlist. lastchild. firstchild. Text + 1
Else
''Otherwise, the ID is equal to 1.
Id = 1
End if
''Output value
''Response. Write (ID)
''Set the sortid value of the subnode
SID = "A" & ID
''Sets the value of the title node of the subnode.
Title = "booktitle"
''Line break
Brstr = CHR (10) & CHR (9)
''Edit new node content
Newxmlnode = brstr & "<book id =" & ID & "" sortid = "& SID &" ">" & vbnewline &_
"<Title>" & Title & "</title>" & vbnewline &_
"<Author> author" & ID & "</author>" & vbnewline &_
"</Book>" & vbnewline
''Create a New XML Object
Set objnewxml = server. Createobject ("Microsoft. xmldom ")
''Load new node content into memory
Objnewxml. loadxml (newxmlnode)
''Get the root node of the objnewxml object
Set rootnewnodemetadata objnewxml.doc umentelement
''Add the new node fragment to the XML file.
Objsortlist. appendchild (rootnewnode)
''Save the XML file
Objxmldom. Save (filestr)
Set objxmldom = nothing
Set objnewxml = nothing
%>
'Page refresh to add a new node
Books. xml
<? XML version = "1.0" encoding = "gb2312"?>
<Books>
<Book id = "0001" sortid = "A1">
<Title> Asp </title>
<Author> AA </author>
</Book>
<Book id = "0002" sortid = "B3">
<Title> JSP </title>
<Author> BBB </author>
</Book>
</Books>