asp對(duì)xml的增、刪、改、查
當(dāng)前位置:點(diǎn)晴教程→知識(shí)管理交流
→『 技術(shù)文檔交流 』
首先看一下xml文件
text.xml
'encoding使用gb2312中文,如果要用英文則用utf-8
<?xml version="1.0" encoding="gb2312"?>
<root>
<row ID="2" COMPNAME="閃電兒" ADDR="河北省唐山市" 郵編="063000" TEL="0315-2695" PRODSERV="網(wǎng)頁(yè)教學(xué)" 經(jīng)濟(jì)行業(yè)="網(wǎng)頁(yè)制作" 省份="河北省" 地市="唐山市" 區(qū)域="路南區(qū)" visit="0" 優(yōu)先級(jí)="888888888" zhuangtai="最新"/>
<row ID="3" COMPNAME="網(wǎng)頁(yè)教學(xué)qwwqq" ADDR="河北省唐山市aaa" 郵編="063000" TEL="0315-2695" PRODSERV="網(wǎng)頁(yè)教學(xué)" 經(jīng)濟(jì)行業(yè)="網(wǎng)頁(yè)制作" 省份="河北省" 地市="唐山市" 區(qū)域="路南區(qū)" visit="0" 優(yōu)先級(jí)="888888888" zhuangtai="最新"/>
<row ID="4" COMPNAME="sdfds" ADDR="sdfds" 郵編="sdfds" TEL="sdfds" PRODSERV="sdfds" 經(jīng)濟(jì)行業(yè)="sdfds" 省份="sdfds" 地市="sdfds" 區(qū)域="sdfds" visit="sdfds" 優(yōu)先級(jí)="sdfds" zhuangtai="sdfds"/>
<row ID="5" COMPNAME="asd" ADDR="asd" 郵編="asd" TEL="asd" PRODSERV="asd" 經(jīng)濟(jì)行業(yè)="asd" 省份="asd" 地市="asd" 區(qū)域="asd" visit="asd" 優(yōu)先級(jí)="asd" zhuangtai="asd"/>
<row ID="6" COMPNAME="m" ADDR="m" 郵編="m" TEL="m" PRODSERV="m" 經(jīng)濟(jì)行業(yè)="m" 省份="m" 地市="m" 區(qū)域="m" visit="m" 優(yōu)先級(jí)="m" zhuangtai="m"/>
</root> 讀取xml文件,如圖
index.asp
<%
path="text.xml"
dim xml,objNode,objAtr,nCntChd,nCntAtr
Set xml=Server.CreateObject("Microsoft.XMLDOM")
xml.validateOnParse = True
xml.Load(Server.MapPath(path))
xml.Async=False
Set objNode=xml.documentElement
nCntChd=objNode.childnodes.length-1
'這個(gè)可以定義asp讀取xml文件的那一個(gè)值,通過(guò)傳遞這個(gè)值來(lái)確定讀取的數(shù)據(jù)
%>
<table width="90%" border="0" cellspacing="0" cellpadding="0">
<tr>
<%
for h=0 to nCntChd
%>
<td height="25" align="center">名稱</td>
<%
next
%>
<td height="25"><a href="addxmlInfo.asp">添加</a> </td>
</tr>
<%
for i=0 to nCntChd
set objAtr=objNode.ChildNodes.item(i)
nCntAtr=objAtr.Attributes.length-1
'歷遍一條記錄里面的所有的記錄項(xiàng),記錄是從0開(kāi)始的
%>
<tr>
<%
for j=0 to nCntAtr
%>
<td height="25" align="center" valign="middle"><%=objAtr.Attributes.item(j).Text%></td>
<%
'response.write objAtr.Attributes.item(j).Text&"<br>"
next
%>
<td height="25" align="center" valign="middle"><a href="modXml.asp?id=<%=i%>">修改</a> <a href="delXml.asp?id=<%=i%>">刪除</a></td>
</tr>
<%
next
%>
</table>
<%
Set objAtr=Nothing
Set objNode=Nothing
Set xml=Nothing
%>
添加xml文件內(nèi)容
addxmlInfo.asp
<%
dim xml,objNode,objAtr,nCntChd,nCntAtr
Set xml=Server.CreateObject("Microsoft.XMLDOM")
xml.validateOnParse = True
xml.Load(Server.MapPath("text.xml"))
xml.Async=False
Set objNode=xml.documentElement
nCntChd=objNode.childnodes.length-1
set objAtr=objNode.ChildNodes.item(nCntChd)
nCntAtr=objAtr.Attributes.length-1
%>
<form action="addxml.asp" method="post">
<table width="43%" border="0" cellspacing="0" cellpadding="0">
<%
for n=0 to nCntAtr
%>
<tr>
<td><%=objAtr.Attributes.item(n).Name%></td>
<%
if objAtr.Attributes.item(n).Name="ID" then
%>
<td align="left"><input type="text" name="<%=objAtr.Attributes.item(n).Name%>" value="<%=cint(objAtr.Attributes.item(n).Text)+1%>" /></td>
<%
else
%>
<td align="left"><input type="text" name="<%=objAtr.Attributes.item(n).Name%>" value="" /></td>
<%
end if
%>
</tr>
<%
next
%>
<tr>
<td><input type="submit" value="添加" /></td>
</tr>
</table>
</form>
<%
Set objAtr=Nothing
Set objNode=Nothing
Set xml=Nothing
%>
addxml.asp
<%
dim xml,objNode,objAtr,nCntChd,nCntAtr
Set xml=Server.CreateObject("Microsoft.XMLDOM")
xml.validateOnParse = True
xml.Load(Server.MapPath("text.xml"))
xml.Async=False
Set objNode=xml.documentElement
set row = xml.createNode(1,"row","")
objNode.appendChild(row)
xml.Save(Server.MapPath("text.xml"))
'最后一個(gè)ID號(hào)
nCntChd=objNode.childnodes.length-1
set objAtr=objNode.ChildNodes.item(nCntChd-1)
nCntAtr=objAtr.Attributes.length-1
for n=0 to nCntAtr
call xml.getElementsByTagName("row").item(xml.DocumentElement.childNodes.length-1).setAttribute(""&objAtr.Attributes.item(n).Name&"",""&trim(request.Form(objAtr.Attributes.item(n).Name))&"")
next
xml.Save(Server.MapPath("text.xml"))
set objAtr=nothing
set row=nothing
set objNode=nothing
set xml=nothing
response.Write "<script>alert('添加成功!');location.href='readXml.asp';</script>"
%> 修改xml文件
modXml.asp
<%
dim id
id=request("id")
if id<>"" then
dim xml,objNode,objAtr,nCntChd,nCntAtr
Set xml=Server.CreateObject("Microsoft.XMLDOM")
xml.validateOnParse = True
xml.Load(Server.MapPath("text.xml"))
xml.Async=False
Set objNode=xml.documentElement
set objAtr=objNode.ChildNodes.item(id)
nCntAtr=objAtr.Attributes.length-1
%>
<form action="modXmlInfo.asp" method="post">
<table width="43%" border="0" cellspacing="0" cellpadding="0">
<%
for n=0 to nCntAtr
%>
<tr>
<td><%=objAtr.Attributes.item(n).Name%></td>
<td align="left"><input type="text" name="Atr<%=n%>" value="<%=objAtr.Attributes.item(n).Text%>" /></td>
</tr>
<%
next
%>
<tr>
<td><input type="submit" value="修改" />
<input name="id" type="hidden" id="id" value="<%=id%>"/></td>
</tr>
</table>
</form>
<%
end if
%>
<%
Set objAtr=Nothing
Set objNode=Nothing
Set xml=Nothing
%> modXmlInfo.asp <%
dim id
id=request("id")
if id<>"" then
dim xml,objNode,objAtr,nCntChd,nCntAtr
Set xml=Server.CreateObject("Microsoft.XMLDOM")
xml.validateOnParse = True
xml.Load(Server.MapPath("text.xml"))
xml.Async=False
Set objNode=xml.documentElement
set objAtr=objNode.ChildNodes.item(id)
nCntAtr=objAtr.Attributes.length-1
for c=0 to nCntAtr
objAtr.Attributes.item(c).Text=request.Form("Atr"&c)
'response.Write request.Form("Atr"&c)
'response.Write "<>"
next
xml.save(Server.mappath("text.xml"))
end if
set objAtr=nothing
set objNode=nothing
set xml=nothing
response.Write "<script>alert('保存成功');location.href='readxml.asp'</script>"
%> 修改完畢,接下來(lái)只剩下刪除了
delXml.asp
<%
dim id
id=request("id")
if id<>"" then
dim xml,objNode,objAtr,nCntChd,nCntAtr
Set xml=Server.CreateObject("Microsoft.XMLDOM")
xml.validateOnParse = True
xml.Load(Server.MapPath("text.xml"))
xml.Async=False
Set objNode=xml.documentElement
objNode.removechild(objNode.ChildNodes.item(id))
xml.save(Server.mappath("text.xml"))
end if
set objAtr=nothing
set objNode=nothing
set xml=nothing
response.Write "<script>alert('保存成功');location.href='readxml.asp'</script>"
%> 該文章在 2012/7/10 16:48:59 編輯過(guò) |
相關(guān)文章
正在查詢... |