ASP操作XML数据小结_第1页
ASP操作XML数据小结_第2页
ASP操作XML数据小结_第3页
ASP操作XML数据小结_第4页
ASP操作XML数据小结_第5页
已阅读5页,还剩101页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、ASP操作XML数据小结添加时间:2007-4-22ASP操作XML数据小结NO.1-建立一个XML数据库NO.2-建立对象CreateObject建立data.xml的对象先setxmldoc=server.createobjcet(microsoft.xmldom)xmldoc.load(server.mappath(data.xml)NO.3-选定节点SelectNode你想操作哪个Node,必须定位到这个节点是不是,先看看这个data.xml有几个Node?用一个递归函数搞定:getnodes(xmldoc)subget

2、nodes(node)dimiresponse.write(NodeName:&node.nodename&NodeTypeString:&node.nodetypestring&NodeValue:&node.nodevalue&Text:&node.text&node.childnodes.length:&node.childnodes.length&)ifnode.childnodes.length0thenfori=0tonode.childnodes.length-1getnodes(node.childnodes(i)nextendifendsub用这个函数后,可以看到这个data

3、.xml有10个Node这些Node可以很简单的定位:xmldocxmldoc.childnodes(0)xmldoc.childnodes(1)xmldoc.childnodes(1).childnodes(0)xmldoc.childnodes(1).childnodes(0).childnodes(0)xmldoc.childnodes(1).childnodes(0).childnodes(0).textxmldoc.childnodes(1).childnodes(0).childnodes(1)xmldoc.childnodes(1).childnodes(0).childnode

4、s(1).textxmldoc.childnodes(1).childnodes(0).childnodes(2)xmldoc.childnodes(1).childnodes(0).childnodes(2).text是不是定位很简单呀,还有个方法,比如定位xmldoc.selectsinglenode(/name)还有:xmldoc.getelementsbytagname(name).item(0)NO.4-给节点赋值(修改节点的值)学会了定位节点,利用其属性,就可以修改或者赋值了例如,把的值caca改为wawaxmldoc.selectsinglenode(/name).text=wa

5、waxmldoc.save(server.mappath(data.xml)搞定!NO.5-创建新的节点CreatenewNode用createelement或者createnode(,)例如:在record下新建个,只需要一句就搞定:xmldoc.selectsinglenode(/record).appendchild(xmldoc.createelement()给赋值xmldoc.selectsinglenode(/age).text=20xmldoc.save(server.mappath(data.xml)搞定!NO.6-删除一个节点DeleteNode你必须明确你想删除的这个节点的

6、父节点,以及这个节点的特征例如:删除节点xmldoc.selectsinglenode(/record).removechild(xmldoc.selectsinglenode(/qq)例如:删除那个=caca的xmldoc.selectsinglenode(/records).removechild(xmldoc.selectsinglenode(/recordname=caca)xmldoc.save(server.mappath(data.xml)搞定!只有能熟练这6条code,用asp控制xml数据库,也就差不多了. ASP操作XML的类2008-03-07 00:36:45 来源:网

7、页教学网 ASP操作XML的类的说明:可以完成ASP对XML节点的添加、删除、修改、清空。进行上述操作,根据条件,操作类型包括:仅对第一个符合条件、及全部符合条件二种。如:将第一条件值替换成新值call xml.r_node(/blog/sorts/sortid=2/name,分lei2)将全部条件值替换成新值call xml.r_nodes(/blog/sorts/sortid=2/name,分lei2)%dim xml,xmlfilexmlfile=/Person/3/7/fls975/blog.xmlset xml=new QT_XML_ClassClass QT_XML_ClassPr

8、ojict : ASP操作XMLAuthor : MissdeLink : Date : 15:25 2007-5-29private dom,xmlpath,docPublic iserr_初始化类Private Sub Class_Initialize()Set dom = Server.CreateObject(Microsoft.FreeThreadedXMLDOM)xmlpath = Server.MapPath(xmlfile)If not dom.Load(xmlpath) ThenSaveToFile &vbcrlf&vbcrlf&vbcrlf&vbc

9、rlf&vbcrlf&vbcrlf&vbcrlf&vbcrlf&,xmlpath dom.Load(xmlpath)End Ifend SubPrivate Sub Class_TerminateIf IsObject(dom) Then Set dom = NothingIf IsObject(doc) Then Set doc = NothingEnd SubPrivate Function SaveToFile(ByVal strBody,ByVal SavePath)dim adoSet ado = Server.CreateObject(ADODB.Stream)ado.Openad

10、o.Type = 2ado.charset = utf-8ado.WriteText strBodyado.SaveToFile SavePath,2ado.CloseSet ado = NothingEnd Functionfunction d_node(node)iserr_=Falseset doc=dom.documentElement.selectSingleNode(node)if not doc is nothing thendoc.parentNode.removeChild(doc)dom.save(xmlpath)iserr_=Trueend ifset doc=nothi

11、ngEnd Functionfunction d_nodes(node)dim iiserr_=Falseset doc=dom.selectNodes(node)if not doc is nothing thenfor i=0 to doc.length-1doc.item(i).parentNode.removeChild(doc.item(i)nextiserr_=Trueend if 网页教学网dom.save(xmlpath)set doc=nothingEnd Functionfunction c_node(node)iserr_=Falseset doc=dom.documen

12、tElement.selectSingleNode(node)if not doc is nothing thendoc.text=dom.Save(xmlpath)iserr_=Trueend ifset doc=nothingend functionfunction c_nodes(node)dim iiserr_=Falseset doc=dom.selectNodes(node)if not doc is nothing thenfor i=0 to doc.length-1doc.item(i).text=nextdom.Save(xmlpath)iserr_=Trueend ifs

13、et doc=nothingend functionfunction checknode(nodes,build)dim doc2,doc3dim i,f_node_,n_node,newnodeiserr_=TrueSet doc = dom.documentElement.selectSingleNode(nodes)if doc is nothing theniserr_=Falseif build thennodes=split(nodes,/)f_node_=n_node=for i=0 to ubound(nodes)-1if nodes(i)= thenf_node_=f_nod

14、e_&nodes(i) 网页教学网 f_node_=f_node_&/n_node=f_node_&nodes(i+1)elsef_node_=n_noden_node=f_node_&/&nodes(i+1)end ifSet doc2 = dom.documentElement.selectSingleNode(f_node_)set doc3 = dom.documentElement.selectSingleNode(n_node)if doc3 is nothing thenSet newnode = dom.createElement(nodes(i+1)newnode.Text=

15、doc2.AppendChild(newnode)Set newnode=nothingend ifset doc2=nothingset doc3=nothingnextdom.Save(xmlpath)end ifend ifset doc=nothingEnd Functionfunction joinxml(inset_node,xmlstr)dim oldxml,newxml,rootNewNodeiserr_=FalseSet oldXML = Server.CreateObject(Microsoft.XMLDOM)oldXML.load(xmlpath)set doc=oldx

16、ml.documentElement.selectSingleNode(inset_node)if not doc is nothing theniserr_=TrueSet newXML = Server.CreateObject(Microsoft.XMLDOM)newXML.loadXML(xmlstr&vbcrlf)set rootNewNode=newXML.documentElement 网页教学网doc.appendChild(rootNewNode)oldxml.Save(xmlpath)end ifset oldXML=nothingset newXML=nothingEnd

17、 Functionfunction r_node(node,newstr)iserr_=Falseset doc=dom.documentElement.selectSingleNode(node)if not doc is nothing thendoc.text=newstriserr_=Trueend ifset doc=nothingdom.Save(xmlpath)End Functionfunction r_nodes(node,newstr)dim iiserr_=Falseset doc=dom.selectNodes(node)if not doc is nothing th

18、enfor i=0 to doc.length-1doc.item(i).text=newstrnextiserr_=Trueend ifset doc=nothingdom.Save(xmlpath)End Functionfunction replace_node(node,newstr)call add_node(node,newstr,0)End Functionfunction add_node(node,newstr,num)set doc=dom.selectNodes(node)if not doc is nothing thenif doc.length =num thenc

19、all d_node(node)end ifend ifset doc=nothingcall joinxml(left(node,instrrev(node,/)-1),newstr) End Functionfunction f_node(node)dim getnodeset doc=dom.documentElement.selectSingleNode(node)if not doc is nothing theniserr_=Truegetnode=doc.Textelsegetnode=iserr_=Falseend ifset doc=nothingf_node=getnode

20、end functionfunction count(node)dim nodenumnodenum=array(0,0,)iserr_=Falseset doc=dom.selectNodes(node)if not doc is nothing thennodenum(0)=doc.lengthnodenum(2)=doc.item(0).xmlif doc.item(0).hasChildNodes() thennodenum(1)=doc.item(0).childNodes.lengthend ifiserr_=Trueend ifcount=nodenumend functionf

21、unction id_(str)id_=String(10-len(str),0)end functionfunction now_(dd)dim m,d,h,mmif not isdate(dd) then d=now()dd=cdate(dd)m=month(dd)d=day(dd)h=hour(dd)mm=Minute(dd)if m10 then m=0&mif d10 then d=0&d Webjx.Comif h10 then h=0&hif mm本文来自:网页教学网()原文链接:/ASP/asp_2008_03_0

22、7_4703.htmlasp操作xml例子-(asp)引用地址:/b679093/d60632080.htm复制超文本复制 返回建站工作室 关闭窗口 窗体顶端11077365银月(观心者) 发表于:07-11-01 16:44 123456 落落花开 颠沛流离,然后满怀希望风来风去 作文欣赏成功不仅要有实力,还.烟台人在西祠的家 烟台随拍白天不懂夜. 心情 Diary 六- 与爱无关混济南的80后 举着花朵去流浪新威海人 剖腹产时的狼狈重现眼前成全我的碧海蓝天 别忘了-你的世界我来过山东聊城星期六. 聊邯排球一家亲成全我的碧海蓝天 幸福是什么?利用XML文件

23、存储数据,可以实现真正的跨平台,给我们的数据实现不同平台的转换带来很大的便利。本文的例子向您展示了如何对XML文件进行数据添加、删除、修改的操作。为了叙述的方便,有时也把本例中的每个人的“个人资料”叫作记录。 首先,我们列出XML文件的所有记录,然后,用服务器端的ASP程序和XSL样式单文件来把XML文件转换到HTML文件里,以便显示在任意浏览器里。 现在假定有一个客户资料文件Userdata.xml如下: 张三 男性 北京市第999号信箱 100000 86-10-88886666 李四 女性 上海市黄浦区1208号黄浦大厦A座11层 20020

24、0 86-21-66668888 杨过 男性 神雕侠侣桃花岛 888888 800-810-8080 小龙女 女 神雕侠侣之古慕派 999999 800-810-9090 我们编写Userdata.xsl样式单文件如下: 客户资料表 姓名 电子邮件 Userdata.asp?mode=viewdetail&email= 增加新记录 XSL样式单文件遍历整个XML文件的内容,并借助HTML的table标记来显示它。这里允许浏览者可以通过点击客户姓名来查看该客户的详细

25、资料,也可以添加新的客户信息。 下面,我们编写Userdata.asp程序来完成XML文件转换,数据的浏览、修改、删除: 我们可以利用下面的文件来完成新客户信息的输入,文件addnewdata.htm如下: 客户信息输入 请输入您的客户信息 姓名: 性别: 地址: 邮政编码: 电话: 电子邮件: 当填写完上面的表单后,提交到数据处理程序processAdd.asp,本程序可以判断Userdata.xml文件是否存在,如果不存在,就建立新文件;否则把提交的数据添加到Userdata.xml中。然后根据提交成功与否,显示数据保存成功与失败。processAdd.asp程序如下: % - 添加数据的

26、函数addNewdatatoXML - Function addNewdatatoXML(strFileName) Dim objDom Dim objRoot Dim objRecord Dim objField Dim objFieldValue Dim objattID Dim objattTabOrder Dim objPI Dim blnFileExists Dim x Set objDom = server.CreateObject(Microsoft.XMLDOM) objDom.preserveWhiteSpace = True blnFileExists = objDom.L

27、oad(strFileName) If blnFileExists = True Then Set objRoot = objDom.documentElement Else Set objRoot = objDom.createElement(客户资料表) objDom.appendChild objRoot End If Set objRecord = objDom.createElement(个人资料) objRoot.appendChild objRecord For x = 1 To Request.Form.Count If instr(1,Request.Form.Key(x),

28、btn) = 0 Then Set objField = objDom.createElement(field) Set objattID = objDom.createAttribute(id) objattID.Text = Request.Form.Key(x) objField.setAttributeNode objattID Set objattTabOrder = objDom.createAttribute(taborder) objattTabOrder.Text = x objField.setAttributeNode objattTabOrder Set objFiel

29、dValue = objDom.createElement(field_value) objFieldValue.Text = Request.Form(x) objRecord.appendChild objField objField.appendChild objFieldValue End If Next If blnFileExists = False then Set objPI = objDom.createProcessingInstruction(xml, version=1.0 encoding=gb2312) objDom.insertBefore objPI, objD

30、om.childNodes(0) End If objDom.save strFileName Set objDom = Nothing Set objRoot = Nothing Set objRecord = Nothing Set objField = Nothing Set objFieldValue = Nothing Set objattID = Nothing Set objattTabOrder = Nothing Set objPI = Nothing End Function On Error Resume Next addNewdatatoXML server.MapPa

31、th(Userdata.xml) If err.number 0 then Response.write(您提交的表单没有成功保存,请重新提交!点击这里重新进行提交。) Else Response.write(您提交的表单已经成功保存!点击这里查看添加后客户资料表) End If % 窗体底端ASP操作XML数据小结 NO.1-建立一个XML数据库data.xml caca 154222225 NO.2-建立对象CreateObject 建立data.xml的对象先 setxmldoc=server.createobjcet(microsoft.xmldom) xmldoc.load(server.mappath(data.xml) NO.3-选定节点SelectNode 你想操作哪个Node,必须定位到这个节点是不是,先看看这个data.xml有几个Node? 用一个递归函数搞定: getnode

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论