XML文件:data.xml
<?xml version="1.0" encoding="gb2312"?> <xml> <admin> <item> <id>1</id> <username>dsadas</username> <password>asdasd</password> </item> <item> <id>2</id> <username>mill</username> <password>123456</password> </item> </admin> </xml>
ASP文件:
<% If request("act")="" Then strSourceFile = Server.MapPath("data.xml") Set objXML = Server.CreateObject("Microsoft.FreeThreadedXMLDOM") objXML.load(strSourceFile) Set objRootsite = objXML.documentElement.selectSingleNode("admin") AllNodesNum = objRootsite.childNodes.length - 1 StarNodes = 0 response.write "<table><tr><td>用户名</td><td>密码</td><td>操作</td></tr>" while StarNodes <= AllNodesNum id = objRootsite.childNodes.item(StarNodes).childNodes.item(0).text username = objRootsite.childNodes.item(StarNodes).childNodes.item(1).text password= objRootsite.childNodes.item(StarNodes).childNodes.item(2).text response.write "<tr><td>"&username&"</td><td>"&password&"</td><td><a href=""?act=del&id="&id&""">删除</a>/<a href=""?act=modify&id="&id&""">修改</a></td></tr>" StarNodes = StarNodes + 1 Wend response.write "</table>" set objXMLTitle = nothing username = Request.Form("username") password =Request.Form("password") If username<>"" And password<>"" Then strSourceFile = Server.MapPath("data.xml") Set objXML = Server.CreateObject("Microsoft.XMLDOM") objXML.load(strSourceFile) If objXML.parseError.ErrorCode <> 0 Then objXML.loadXML "<?xml version=""1.0"" encoding=""gb2312"" ?><xml><admin></admin></xml>" End If Set objRootlist = objXML.documentElement.selectSingleNode("admin") If objRootlist.hasChildNodes then id = objRootlist.lastChild.firstChild.text + 1 Else id=1 End If Set oListNode = objXML.documentElement.selectSingleNode("admin").AppendChild(objXML.createElement("item")) Set oDetailsNode = oListNode.appendChild(objXML.createElement("id")) oDetailsNode.Text = id Set oDetailsNode = oListNode.appendChild(objXML.createElement("username")) oDetailsNode.Text = username Set oDetailsNode = oListNode.appendChild(objXML.createElement("password")) oDetailsNode.Text = password objXML.save(strSourceFile) Set objXML=Nothing Response.Redirect "index.asp" End If %> <Form action="" method="post" name="form"> 用户名:<input name="username" type="text"><br> 密 码:<input name="password" type="text"><br> <input name="submit" type="submit" value="添加"> </form> <% ElseIf request("act")="del" Then did = Request.Querystring("id") sourceFile = Server.MapPath("data.xml") if did<>"" then Set source = Server.CreateObject("Msxml2.DOMDocument") source.async = false source.load(sourceFile) Set currNode = source.selectSingleNode("xml/admin/item[id='" & did & "']") if Not IsNull(currNode) then currNode.parentNode.removeChild(currNode) End If source.save(sourceFile) Response.Redirect "index.asp" Response.end end If
ElseIf request("act")="modify" Then filePath = "data.xml" Set objXML = server.CreateObject("Msxml2.DOMDocument") objXML.async = False loadResult = objXML.Load(server.MapPath(filePath)) If Not loadResult Then Response.Write ("加载XML文件出错!") Response.End End If
i = Request.QueryString("ID") Set objNodes = objXML.selectSingleNode("xml/admin/item[id ='" & i & "']") if Not IsNull(objNodes) Then If request("submit")="修改" Then objNodes.childNodes(1).text = request("username") objNodes.childNodes(2).text = request("password") objXML.save(server.MapPath(filePath)) Set objXML=nothing Response.Redirect "index.asp" response.end end If username = objNodes.childNodes(1).text password = objNodes.childNodes(2).text response.write "<Form action="""" method=""post"" name=""form"">" response.write "用户名:<input name=""username"" type=""text"" value="""&username&"""><br>" response.write "密 码:<input name=""password"" type=""text"" value="""&password&"""><br>" response.write "<input name=""submit"" type=""submit"" value=""修改"">" response.write "</form>" End If End If %> |