Asp+xml===》制作个简单的留言本 三个asp文件,与一个xml

index.asp  显示xml文件与分页
<%
 strSourceFile = Server.MapPath("./") & "\List.xml"
 '获取XML文件的路径这里根据你的虚拟目录不同而不同
 Set objXML = Server.CreateObject("Microsoft.XMLDOM")
 '以自由线程创建一个XML对像
 objXML.load(strSourceFile)'把XML文件读入内存
 Set objRootsite = objXML.documentElement.selectSingleNode("NewList")
 '选取NewList节点
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
 <style type="text/css">
<!--
td { font-family: "宋体"; font-size: 9pt; text-decoration: none}
a { font-family: "宋体"; font-size: 9pt; color: #0066CC; text-decoration: none}
a:hover { color: #FF6600; text-decoration: underline}
-->
</style>
</head><body bgcolor="#0099CC" text="#000000">
<table width="80%" border="0" cellspacing="1" cellpadding="4" align="center" bgcolor="#CCCCCC">
<tr bgcolor="#000000">
<td colspan="2"><font color="#FFFFFF">留言溥 <a href="PostNew.asp"><font color=#FFFFFF>写新留言
</font></a></font></td>
</tr>
<%
 PageSize =3 '假设每页显示10条留言
 AllNodesNum =objRootsite.childNodes.length-1
'response.write objRootsite.childNodes.length
'response.end
 '获取子节点数据(因为是从节点数从0开始的所最大子节点数要减1)
 PageNum=AllNodesNum\PageSize+1'算出总页数
 PageNo=request.querystring("PageNo")

 if PageNo="" then'如果是每一次获得页面则定位到每一页显示最新的留言
  PageNo=PageNum
 end if
 
 StarNodes=PageNo*PageSize-1'获得起始节点
 EndNodes=(PageNo-1)*PageSize'获得结束节点

 if EndNodes<0 then
  EndNodes=0
 end if
'response.write " StarNodes and AllNodesNum==="&StarNodes &"----"&AllNOdesNum&"<br>"
 if StarNodes>AllNodesNum then'判断起始节点数是否超过总的节点数
  EndNodes=EndNodes-(StarNodes-AllNodesNum)
  '如果超过则结束节点要减去(StarNodes-AllNodesNum)的差值否则下标会超界出错
  StarNodes=AllNodesNum
 end if
'response.write "StarNodes=_________"&StarNodes &"---"&"<br>"
 if EndNodes<0 then
  EndNodes=0
 end if
 while StarNodes>=EndNodes
'从结束节点到超始节点之间读取节点数据
'response.write "id="&objRootsite.childNodes.item(StarNodes).childNodes.item(0).text

  id   = objRootsite.childNodes.item(StarNodes).childNodes.item(0).text
'id号 
  username = objRootsite.childNodes.item(StarNodes).childNodes.item(1).text
'获得用户名
  fromwhere = objRootsite.childNodes.item(StarNodes).childNodes.item(2).text
'获得用户来自那里
  Posttime = objRootsite.childNodes.item(StarNodes).childNodes.item(3).text
'获得留言时间
  homepage = objRootsite.childNodes.item(StarNodes).childNodes.item(4).text
'获得用户主页
  email  = objRootsite.childNodes.item(StarNodes).childNodes.item(5).text
'获得用户Eamil
  text  = objRootsite.childNodes.item(StarNodes).childNodes.item(6).text
'获昨留言内容
'*****************************************************************************
'这里使作了XML的DOM来读取数据,显然objRootsite对像所对应的节点为<NewList>
' objRootsite.childNodes.item(StarNodes)所对应的节点就是<list>节点因为不至一个<list>节点所以用了item()
'来识别当前节点数据,随着StarNodes的递减<list>节点根着往上移一个个读取<list>节点数据。
'objRootsite.childNodes.item(StarNodes).childNodes.item(1).text所对应的节点为具体的每个节点的文本值,这里
'(1)所对应的就是username用户名(因为<list>节点的子节点才是我们真正所要的)
'******************************************************************************
  text = replace(text,chr(13),"<br>")
'替代回车
  text = replace(text,chr(32)," ")
'替代空格
%>
<tr bgcolor="#F0F0F0">
<td width="21%" height="94" valign="top">id号:<%=id%><br>姓名:<%=username%><br>
来自:<%=fromwhere%><br>
<a href='PostEdit.asp?id=<%=id%>'>编辑留言</a>
</td>
<td width="79%" height="94" valign="top"> | <a href="<%=homepage%>" target=_blank title="<%=username%>的主页">主页</a>
| | <a href="mailto:<%=email%>" title="给<%=username%>写信">信箱</a> | |<font color="#CC6633">
留言时间:<%=Posttime%> </font>|
<hr>
<%=text%> </td>
</tr>
<tr bgcolor="#FFFFFF" align="right">
<td colspan="2"> </td>
</tr>
<%
StarNodes=StarNodes-1
wend
set objXML=nothing
%>
<tr bgcolor="#FFFFFF" align="right">
<td colspan="2"> 共有<<%=PageNum%>>页
<%
if cint(PageNo)<>PageNum then '分页
 response.write "<a href=index.asp?PageNo="&(PageNo+1)&">上一页</a>"
 end if
if cint(PageNo)<>1 then
 response.write "<a href=index.asp?PageNo="&(PageNo-1)&">下一页</a>"
end if
%>
</td>
</tr>
</table>
</body>
</html>
  
================================================================================
PostNew.asp  添加留言内容

<%
username=request.form("username")
if username<>"" then
 fromwhere =request.form("fromwhere")
 homepage =request.form("homepage")
 email =request.form("email")
 text =request.form("text")
 text =replace(text,"<","<")
 Posttime =now()
 strSourceFile = Server.MapPath("./") & "\List.xml"
 '获取XML文件的路径这里根据你的虚拟目录不同而不同
 Set objXML = Server.CreateObject("Microsoft.XMLDOM")
 '创建XML对像
 objXML.load(strSourceFile)
 '把XML文件读入内存中
 Set objRootlist = objXML.documentElement.selectSingleNode("NewList")
 '选取<NewList>节点
 if objRootlist.hasChildNodes then
 '判断<NewList>是否有子节点(因为如果是每一次<NewList>是没有子节点的, 如果不加判断在第一次运得时就会报错
  id = objRootlist.lastchild.firstchild.text+1
 '这里获得将要插入子节点的ID号,其ID号为<NewList>的最后一个子节点(lastchild)的第一个子节点(firstchild)
 '的ID号加1(这里我们按照关系型数据库的ID号来递增)
 else
 '如是没有字子节点则是第一次留言ID号设为1
  id=1
 end if
 brstr=chr(13)&chr(10)&chr(9)
 '为了插入XML文件中的节点换行空格排列整齐
 '(当然你也可以不用这样只是为了XML数据好看而以)
 XMLnode=brstr&"<list>"&brstr & _
  "<id>"&id&"</id>"&brstr & _
  "<username>"&username&"</username>"&brstr & _
  "<fromwhere>"&fromwhere&"</fromwhere>"&brstr & _
  "<Posttime>"&Posttime&"</Posttime>"&brstr & _
  "<homepage>"&homepage&"</homepage>"&brstr & _
  "<email>"&email&"</email>"&brstr & _
  "<text>"&text&"</text>"&brstr & _
  "</list>"&chr(13)
 '根据得到的数据建立XML片段
 set objXML2=Server.CreateObject("Microsoft.XMLDOM")
 '建立一个新XML对像
 objXML2.loadXML(XMLnode)
 '把XML版片段读入内存中
 set rootNewNode=objXML2.documentElement
 '获得objXML2的根节点
 objRootlist.appendChild(rootNewNode)
 '把XML片段插入到List.xml中
 objXML.save(strSourceFile)
 '存储lsit.xml文件(因为不存储List.xml只在内存中更新了)
 set objXML=nothing
 set objXML2=nothing
 Response.Redirect("index.asp")
 response.end
end if
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="Author" content="许诺,xunuo">
<title>网络桌面(xml留言板) - http://www.xunuo.net</title>
<script language="javascript" src="other.js"></script>
<style type="text/css">
<!--
td { font-size: 9pt}
-->
</style>
</head>
<body bgcolor="#0099CC" text="#000000">
<table width="80%" border="0" cellspacing="1" cellpadding="4" align="center" bgcolor="#FFFFFF">
<form action="PostNew.asp" method="post" name="form1">
<tr bgcolor="#000000">
<td colspan="2"><font color="#FFFFFF">新留言</font></td>
</tr>
<tr bgcolor="#EFEFEF">
<td width="19%" align="right">姓 名:</td>
<td width="81%">
<input type="text" name="username">
** </td>
</tr>
<tr bgcolor="#EFEFEF">
<td width="19%" align="right">来 自:</td>
<td width="81%">
<input type="text" name="fromwhere" value="中国">
</td>
</tr>
<tr bgcolor="#EFEFEF">
<td width="19%" align="right">主 页:</td>
<td width="81%">
<input type="text" name="homepage" value="http://">
</td>
</tr>
<tr bgcolor="#EFEFEF">
<td width="19%" align="right">Email:</td>
<td width="81%">
<input type="text" name="email">
</td>
</tr>
<tr bgcolor="#EFEFEF">
<td width="19%" align="right" valign="top">内 容:</td>
<td width="81%">
<textarea name="text" cols="60" rows="10"></textarea>
</td>
</tr>
<tr bgcolor="#EFEFEF">
<td width="19%" align="right"> </td>
<td width="81%">
<input type="submit" name="Submit" value="提交">
<input type="reset" name="Submit2" value="重填">
</td>
</tr>
</form>
</table>
</body>
</html>
========================================================================================
postedit.asp 编辑留言内容

<%
 dim id ,action
  id = Request.QueryString("id")
if IsNumeric(id)=false or isNull(id) then
 response.write ("参数不正确,请返回!")
 response.end
end if
 action = Request.QueryString("action")
id = id
if action = "update" then
 id  = request("id")
 username=request.form("username")
 fromwhere =request.form("fromwhere")
 homepage =request.form("homepage")
 email =request.form("email")
 text =request.form("text")
 text =replace(text,"<","<")
 Posttime =now()
 strSourceFile = Server.MapPath("./") & "\List.xml"
 '获取XML文件的路径这里根据你的虚拟目录不同而不同
 Set objXML = Server.CreateObject("Microsoft.XMLDOM")
 '创建XML对像
 objXML.load(strSourceFile)
 '把XML文件读入内存中
 Set objRootlist = objXML.documentElement.selectSingleNode("NewList")
 '选取<NewList>节点
 
 set rootOldNode=objRootlist.childNodes.item(id)
'--------NEW START -----------
  brstr=chr(13)&chr(10)&chr(9)
 XMLnode=brstr&"<list>"&brstr & _
  "<id>"&id&"</id>"&brstr & _
  "<username>"&username&"</username>"&brstr & _
  "<fromwhere>"&fromwhere&"</fromwhere>"&brstr & _
  "<Posttime>"&Posttime&"</Posttime>"&brstr & _
  "<homepage>"&homepage&"</homepage>"&brstr & _
  "<email>"&email&"</email>"&brstr & _
  "<text>"&text&"</text>"&brstr & _
  "</list>"&chr(13)
 '根据得到的数据建立XML片段
 set objXML2=Server.CreateObject("Microsoft.XMLDOM")
 '建立一个新XML对像
 objXML2.loadXML(XMLnode)
 '把XML版片段读入内存中
 set rootNewNode=objXML2.documentElement
'===========NEW=============
 objRootlist.ReplaceChild rootNewNode,rootOldNode
 objXML.save(strSourceFile)
 set objXML=nothing
 set objXML2=nothing
 Response.Redirect("index.asp")
 response.end
else
 strSourceFile = Server.MapPath("./") & "\List.xml"
 '获取XML文件的路径这里根据你的虚拟目录不同而不同
 Set objXML = Server.CreateObject("Microsoft.XMLDOM")
 '创建XML对像
 objXML.load(strSourceFile)
 '把XML文件读入内存中
 Set objRootsite = objXML.documentElement.selectSingleNode("NewList")
 '选取<NewList>节点
' response.write objRootsite.childNodes.item(id).childNodes.item(1).text
' response.end
  id   = objRootsite.childNodes.item(id).childNodes.item(0).text
'id号 
  username = objRootsite.childNodes.item(id).childNodes.item(1).text
'获得用户名
  fromwhere = objRootsite.childNodes.item(id).childNodes.item(2).text
'获得用户来自那里
  Posttime = objRootsite.childNodes.item(id).childNodes.item(3).text
'获得留言时间
  homepage = objRootsite.childNodes.item(id).childNodes.item(4).text
'获得用户主页
  email  = objRootsite.childNodes.item(id).childNodes.item(5).text
'获得用户Eamil
  text  = objRootsite.childNodes.item(id).childNodes.item(6).text 
 set objXML = nothing
end if
%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="Author" content="许诺,xunuo">
<title>网络桌面(xml留言板) - http://www.xunuo.net</title>
<script language="javascript" src="other.js"></script>
<style type="text/css">
<!--
td { font-size: 9pt}
-->
</style>
</head>
<body bgcolor="#0099CC" text="#000000">
<table width="80%" border="0" cellspacing="1" cellpadding="4" align="center" bgcolor="#FFFFFF">
<form action="PostEdit.asp?action=update" method="post" name="form1">
<input type="hidden" value="<%=id%>" name="id" />
<tr bgcolor="#000000">
<td colspan="2"><font color="#FFFFFF">新留言</font></td>
</tr>
<tr bgcolor="#EFEFEF">
<td width="19%" align="right">姓 名:</td>
<td width="81%">
<input type="text" name="username" value="<%=username%>">
** </td>
</tr>
<tr bgcolor="#EFEFEF">
<td width="19%" align="right">来 自:</td>
<td width="81%">
<input type="text" name="fromwhere"  value="<%=fromwhere%>">
</td>
</tr>
<tr bgcolor="#EFEFEF">
<td width="19%" align="right">主 页:</td>
<td width="81%">
<input type="text" name="homepage"  value="<%=homepage%>">
</td>
</tr>
<tr bgcolor="#EFEFEF">
<td width="19%" align="right">Email:</td>
<td width="81%">
<input type="text" name="email" value="<%=email%>">
</td>
</tr>
<tr bgcolor="#EFEFEF">
<td width="19%" align="right" valign="top">内 容:</td>
<td width="81%">
<textarea name="text" cols="60" rows="10"><%=text%></textarea>
</td>
</tr>
<tr bgcolor="#EFEFEF">
<td width="19%" align="right"> </td>
<td width="81%">
<input type="submit" name="Submit" value="提交">
<input type="reset" name="Submit2" value="重填">
</td>
</tr>
</form>
</table>
</body>
</html>

=====================================================
listxml.xml文件
 
<?xml version="1.0" encoding="gb2312"?>
<site>
<NewList>
  <list>
   <id>0</id>
   <username>许诺</username>
   <fromwhere>中国湖北</fromwhere>
   <Posttime>2006-7-3 10:12:42</Posttime>
   <homepage>http://www.xunuo.net/</homepage>
   <email>lffxunuo@163.com</email>
   <text>新版asp+xml留言板。改正了前一版编辑出错地方。</text>
  </list>
  <list>
        <list>
   <id>5</id>
   <username>eeee姓 名</username>
   <fromwhere>中国</fromwhere>
   <Posttime>2006-12-15 16:01:47</Posttime>
   <homepage>http://</homepage>
   <email>Email:</email>
   <text>内 容:</text>
  </list>
  <list>
   <id>6</id>
   <username>新留言 </username>
   <fromwhere>中国</fromwhere>
   <Posttime>2006-12-15 16:02:42</Posttime>
   <homepage>http://</homepage>
   <email>Email:</email>
   <text>内 容: </text>
  </list>
 </NewList>
</site>

原文地址:https://www.cnblogs.com/winner/p/593441.html