ASP operations xml--to add nodes to an XML file

Source: Internet
Author: User
Tags chr

<! 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 (&AMP;CHR) &AMP;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>

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.