ASP XMLDom在服务器端操作XML文件的主要方法和实现
来源:asp之家 时间:2011-03-09 11:09:00
对于小数据量,xml文件在检索更新上于ACCESS有很多优势。
我曾经测试过不用数据库,把网站的会员信息,商品数据信息,交易信息,网站定制信息全部存放在三个xml文件中,运行结果十分正常,感觉上比数据库快多了,不过没有作测试,不能确定。
下面说一下创建,查询,修改等对xml操作的主要方法
程序代码
NO.1--建立一个XML数据库data.xml
代码如下:
<?xml version="1.0"?>
<records>
<record>
<name>caca</name>
<qq>154222225</qq>
<email>root@3ney.com</email>
</record>
<records>
NO.2--建立对象CreateObject
建立data.xml的对象先
set xmldoc=server.createobjcet("microsoft.xmldom")
xmldoc.load(server.mappath("data.xml")
NO.3--选定节点SelectNode
你想操作哪个Node,必须定位到这个节点是不是,先看看这个data.xml有几个Node??
用一个递归函数搞定:
代码如下:
getnodes(xmldoc)
sub getnodes(node)
dim i
response.write("<br><b>NodeName:</b>"&node.nodename&"<br><b>NodeTypeString:</b>"&node.nodetypestring&"<br><b>NodeValue:</b>"&node.nodevalue&"<br><b>Text:</b>"&node.text&"<br><b>node.childnodes.length:</b>"&node.childnodes.length&"<p>")
if node.childnodes.length<>0 then
for i=0 to node.childnodes.length-1
getnodes(node.childnodes(i))
next
end if
end sub
用这个函数后,可以看到这个data.xml有10个Node
这些Node可以很简单的定位:
xmldoc
xmldoc.childnodes(0)
xmldoc.childnodes(1)
xmldoc.childnodes(1).childnodes(0)
xmldoc.childnodes(1).childnodes(0).childnodes(0)
xmldoc.childnodes(1).childnodes(0).childnodes(0).text
xmldoc.childnodes(1).childnodes(0).childnodes(1)
xmldoc.childnodes(1).childnodes(0).childnodes(1).text
xmldoc.childnodes(1).childnodes(0).childnodes(2)
xmldoc.childnodes(1).childnodes(0).childnodes(2).text
是不是定位很简单呀,还有个方法,比如定位<name>
xmldoc.selectsinglenode("//name")
NO.4--给节点赋值(修改节点的值)
学会了定位节点,利用其属性,就可以修改或者赋值了
例如,把<name>的值caca改为wawa
xmldoc.selectsinglenode("//name").text="wawa"
xmldoc.save(server.mappath("data.xml"))
搞定!
NO.5--创建新的节点CreatenewNode
用createelement或者createnode("","","")
例如:在record下新建个<age>,只需要一句就搞定:
xmldoc.selectsinglenode("//record").appendchild(xmldoc.createelement("<age>"))
给<age>赋值
xmldoc.selectsinglenode("//age").text="20"
xmldoc.save(server.mappath("data.xml"))
搞定!
NO.6--删除一个节点DeleteNode
你必须明确你想删除的这个节点的父节点,以及这个节点的特征
例如:删除<qq>节点
xmldoc.selectsinglenode("//record").removechild(xmldoc.selectsinglenode("//qq"))
例如:删除那个<name>=caca的<record>
xmldoc.selectsinglenode("//records").removechild(xmldoc.selectsinglenode("//record[name='caca']))
xmldoc.save(server.mappath("data.xml"))
搞定!
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
浅谈终端直接执行py文件,不需要python命令
Asp生成RSS的类_给网站加上RSS
完美解决python遍历删除字典里值为空的元素报错问题
python 读取Linux服务器上的文件方法
仿淘宝星级评分效果
Python基础面向对象之继承与派生详解
![](https://img.aspxhome.com/file/2023/4/67424_0s.png)
python sklearn库实现简单逻辑回归的实例代码
python3+PyQt5图形项的自定义和交互 python3实现page Designer应用程序
![](https://img.aspxhome.com/file/2023/6/99466_0s.png)
python实现rsa加密实例详解
Python入门教程(二十六)Python的模块
python异常处理和日志处理方式
python 使用百度AI接口进行人脸对比的步骤
![](https://img.aspxhome.com/file/2023/7/68477_0s.png)
纯JS实现动态时间显示代码
go语言代码生成器code generator使用示例介绍
![](https://img.aspxhome.com/file/2023/0/125070_0s.png)
Go实现基于RSA加密算法的接口鉴权
Python Flask搭建yolov3目标检测系统详解流程
![](https://img.aspxhome.com/file/2023/3/95203_0s.gif)
php使用composer常见问题及解决办法
![](https://img.aspxhome.com/file/2023/8/55398_0s.jpg)
HTML,CSS和JavaScript速查表
![](https://img.aspxhome.com/file/UploadPic/20109/16/01-55s.jpg)