1.XML =========================================================================== <?xml version="1.0" encoding="gb2312"?> <data> <tomcat> <ID>1</ID> <username>张三</username> <password>123456</password> </tomcat> </data> ============================================================================
aspXML.asp ============================================================================ <% Class ASPXML Private objDom,objnodes,objTable,Xml_Array,XMLnodes,ID,brstr,NewXMLnode,NewXMLcooky Public Path,Data,Xml_Eof,Xml_Bof,Xml_Item,Xml_Count
'打开文档 Public Sub open() set objDom=server.CreateObject("Microsoft.XMLDOM") objDom.load Path set objnodes=objdom.documentElement.SelectSingleNode("//"+Data+"").ChildNodes set XMLnodes=objdom.documentElement.SelectSingleNode("//"+Data+"") end sub
'选择表 Public Function SelectTable(Table) objTable=Table Xml_Count=-1 Xml_Eof=false Xml_Bof=false 'Xml_Item=2 if Xml_Item="" then Xml_Item=0 end if GETCount() ChkCount() end Function
'下移一行 Public Sub MoveNext() Xml_Item=Xml_Item+1 ChkCount() end sub '上移一行 Public Sub MoveLast() Xml_Item=Xml_Item-1 ChkCount() end sub
'读出值 Public Function Read(key) Read=objnodes(int(Xml_Array(Xml_Item))).selectSingleNode(key).text end Function '写入 Public Function Write(key,viold) objnodes(int(Xml_Array(Xml_Item))).selectSingleNode(key).text=viold end Function '查询字段 Public Function SelectItem(key,viold) Xml_AllTableWords="" for i=0 to Xml_Count xml_viold=objnodes(int(Xml_Array(i))).selectSingleNode(key).text if xml_viold=viold then Xml_AllTableWords=Xml_AllTableWords+"|"+Xml_Array(i) end if next StrPtr = instr(Xml_AllTableWords, "|") Xml_AllTableWords = mid(Xml_AllTableWords, StrPtr+1) Xml_Array=split(Xml_AllTableWords , "|") Xml_Count=UBound(Xml_Array) ChkCount() end Function
'新加记录 Public sub addnew() NewXMLnode="" if XMLnodes.hasChildNodes then ID = XMLnodes.lastchild.firstchild.text+1 else '如是没有字子节点则是第一次留言ID号设为1 ID=1 end if brstr=chr(13)&chr(10)&chr(9) NewXMLcooky=brstr&"<"&objTable&">"&brstr &"<ID>"&ID&"</ID>"&brstr end sub '添加数值 Public Function add(key,viold) NewXMLcooky=NewXMLcooky&"<"&key&">"&viold&"</"&key&">"&brstr NewXMLnode=NewXMLcooky&"</"&objTable&">"&chr(13) end Function '删除记录 Public Function delect(key,viold) for i=0 to Xml_Count if ISkeyOk(key,viold) then XMLnodes.removechild(objDom.selectsinglenode("//"&objTable&"["&key&"='"&viold&"']")) end if next objDom.save(Path) end Function
'删除表 Public Function delectTable() for i=0 to Xml_Count XMLnodes.removechild(objDom.selectsinglenode("//"&objTable)) next objDom.save(Path) end Function
'更新修改表 Public Sub update() if NewXMLnode<>"" then set objXML2=Server.CreateObject("Microsoft.XMLDOM") '建立一个新XML对像 objXML2.loadXML(NewXMLnode) '把XML版片段读入内存中 set rootNewNode=objXML2.documentElement '获得objXML2的根节点 XMLnodes.appendChild(rootNewNode) end if objDom.Save(Path) end sub '关闭 Public Sub CLOSE() set objnodes=nothing set objDom=nothing ' Response.Write(objTable) end sub
'===================================================================== Private Function GETCount() Xml_AllTableWords="" Xml_AllCount=-1 for each element in objnodes Xml_AllCount=Xml_AllCount+1 If element.nodename =objTable then Xml_AllTableWords=Xml_AllTableWords+"|"+cstr(Xml_AllCount) end if next StrPtr = instr(Xml_AllTableWords, "|") Xml_AllTableWords = mid(Xml_AllTableWords, StrPtr+1) Xml_Array=split(Xml_AllTableWords , "|") Xml_Count=UBound(Xml_Array) end Function
Private Function ChkCount() if Xml_Count<0 then Xml_Bof=true end if if Xml_Item>Xml_Count then Xml_Eof=true end if end Function
Private Function ISkeyOk(key,viold) ISkeyOk=false if not(XMLnodes.hasChildNodes) then exit Function end if
for each timent in objnodes If timent.nodename =objTable then set pobjnodes=timent.ChildNodes for each yyment in pobjnodes if yyment.nodename=key and yyment.text=viold then ISkeyOk=true exit Function end if next end if next end Function End Class %> =======================================================================
使用这个类的例子 test.asp ======================================================================= <!--#include file="aspXML.asp"--> <%
set tt=new ASPXML tt.Path=server.MapPath("1.xml")'第一步先选择文件路径 tt.Data="data" '第二步选择数据库 tt.open '打开 tt.SelectTable("tomcat") '选择表 'tt.delect "username","张辉" '这里表示删除 username字段里是 张辉 的所有值 'tt.delectTable() '删除这个表 tt.SelectItem "ID","2" '这里是查询ID为2的记录 'tt.Xml_Item=tt.Xml_Count 'tt.Xml_Item 是第几条记录 tt.Xml_Count是记录总数 tt.addnew ' 添加记录 tt.add "username","张辉" '写入值 tt.add "password","123456" '写入值 tt.update '保存记录 GG=tt.read("username") '读出记录 tt.close '关闭 Response.Write(GG) Response.Write("<br/>") Response.Write("<br/>") '再写个循环读出记录
set HH=new ASPXML HH.Path=server.MapPath("1.xml") '第一步先选择文件路径 HH.Data="data" '第二步选择数据库 HH.open '打开 HH.SelectTable("tomcat") '选择表 HH.SelectItem "username","张辉" '查询条件 如果读出所有 这里不要 do while not(HH.Xml_Bof or HH.Xml_Eof)
Response.Write HH.read("username") Response.Write("|") Response.Write HH.read("password") Response.Write("<br/>")
HH.MoveNext() loop
'写一个添加数据的例子
username=Request.Form("username") password=Request.Form("password")
set HH=new ASPXML HH.Path=server.MapPath("1.xml") '第一步先选择文件路径 HH.Data="data" '第二步选择数据库 HH.open '打开 HH.SelectTable("tomcat") '选择表 hh.Addnew hh.add "username",username hh.add "password",password HH.Update HH.CLOSE
'写一个删除的例子
ID=Request.QueryString("ID")
set HH=new ASPXML HH.Path=server.MapPath("1.xml") '第一步先选择文件路径 HH.Data="data" '第二步选择数据库 HH.open '打开 HH.SelectTable("tomcat") '选择表
HH.delect "ID", ID HH.CLOSE %> |