<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >
<HTML>
<HEAD>
<TITLE> Add new nodes to XML </TITLE>
<meta name= "generator" content= "EditPlus" >
<meta name= "Author" content= "" >
<meta name= "Keywords" content= "" >
<meta name= "Description" content= "" >
<style>
body {font-size:12px;}
</style>
</HEAD>
<BODY>
<!--#include file= "top.asp"-->
<%
Addok=request ("Addok")
If addok= "" Or IsNull (Addok) Then
%>
<form name= "Form1" method= "Post" action= "Addok=add" >
Website name: <input type= "text" name= "site" size= ">"
URL: <input type= "text" name= "url" size= ">"
<input type= "Submit" value= "submitted" > <input type= "reset" value= "reset" >
</form>
<%
End If
If addok= "Add" then
Site=request ("Site")
Url=request ("url")
strSourceFile = Server.MapPath ("./") & "\test.xml"
' Get the path to the XML file, which is different depending on your virtual directory.
Set objxml = Server.CreateObject ("Microsoft.XMLDOM")
' Create an XML pair like
Objxml.load (strSourceFile)
' Read the XML file into memory
Set objrootlist = ObjXML.documentElement.selectSingleNode ("root")
' Select <root> node
If Objrootlist.haschildnodes Then
' Judge <NewList> if there are child nodes (because if each <root> is not a child node, if you do not add a judgment on the first shipment will be an error
id = objrootlist.lastchild.firstchild.text+1
' Here gets the ID number of the child node that will be inserted, with the ID number of the first child node (firstchild) of the Last child node (lastchild) of <root>
' ID number plus 1 (here we increment according to the ID number of the relational database)
Else
' If no word child node is the first time message ID number set to 1
Id=1
End If
BRSTR=CHR (&CHR) &CHR (9)
xmlnode=brstr& "<list>" &vbnewline & _
"<id>" &id& "</id>" &vbnewline & _
"<name>" &site& "</name>" &vbnewline & _
"<url>" &url& "</url>" &vbnewline & _
"<time>" &replace (Replace (now (), "Morning", ""), "Afternoon", "" "&" </time> "&vbnewline & _
"</list>" &vbnewline&vbnewline
' Create an XML fragment based on the resulting data
Set Objxml2=server.createobject ("Microsoft.XMLDOM")
' Create a new XML pair like
Objxml2.loadxml (XMLnode)
' Read the XML fragment into memory
Set Rootnewnode=objxml2.documentelement
' Get the root node of objXML2
Objrootlist.appendchild (Rootnewnode)
' Insert the XML fragment into the Test.xml
Objxml.save (strSourceFile)
' Store Test.xml file (because no storage test.xml is updated in memory only)
Set objxml=nothing
Set objxml2=nothing
Response.Redirect ("index.asp")
Response.End
End If
%>
</BODY>
</HTML>
The contents of the Test.xml are
<?xml version= "1.0" encoding= "gb2312"?>
<site>
<root>
<list>
<id>0</id>
<name> Sina </name>
<url>http://www.sina.com.cn</url>
<time>2007-11-1 10:10:10</time>
</list>
</root>
</site>