<?xml version="1.0" encoding="gb2312"?>
<root>
<row
ID="2" COMPNAME="闪电儿" ADDR="河北省唐山市" 邮编="063000" TEL="0315-2695"
PRODSERV="网页教学" 经济行业="网页制作" 省份="河北省" 地市="唐山市" 区域="路南区" visit="0"
优先级="888888888" zhuangtai="最新"/>
<row
ID="3" COMPNAME="网页教学qwwqq" ADDR="河北省唐山市aaa" 邮编="063000"
TEL="0315-2695" PRODSERV="网页教学" 经济行业="网页制作" 省份="河北省" 地市="唐山市" 区域="路南区"
visit="0" 优先级="888888888" zhuangtai="最新"/>
<row
ID="4" COMPNAME="sdfds" ADDR="sdfds" 邮编="sdfds" TEL="sdfds"
PRODSERV="sdfds" 经济行业="sdfds" 省份="sdfds" 地市="sdfds" 区域="sdfds"
visit="sdfds" 优先级="sdfds" zhuangtai="sdfds"/>
<row
ID="5" COMPNAME="asd" ADDR="asd" 邮编="asd" TEL="asd" PRODSERV="asd"
经济行业="asd" 省份="asd" 地市="asd" 区域="asd" visit="asd" 优先级="asd"
zhuangtai="asd"/>
<row
ID="6" COMPNAME="m" ADDR="m" 邮编="m" TEL="m" PRODSERV="m" 经济行业="m"
省份="m" 地市="m" 区域="m" visit="m" 优先级="m" zhuangtai="m"/>
</root>
<%
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
‘这个可以定义asp读取xml文件的那一个值,通过传递这个值来确定读取的数据
%>
<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
‘历遍一条记录里面的所有的记录项,记录是从0开始的
%>
<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文件内容
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"))
‘最后一个ID号
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>"
%>