Asp操作Xml的精炼类,含示例代码
以下保存成 app.xml , 与asp文件放在相同目录下!
代码如下:

<?xml version="1.0" encoding="utf-8"?>
<root>
<about>
<version>1.0 beta</version>
<latestversion>1.0 beta</latestversion>
<author>author</author>
<pubdate>2010/02/20</pubdate>
</about>
<config>
<installed>false</installed>
<bakpath>_data</bakpath>
</config>
</root>

以下为asp类及使用方法,请保存成test.asp, 测试运行
代码如下:

<%
class appconfig
dim xmldom
private sub class_initialize()
set xmldom = server.createobject("microsoft.xmldom")
xmldom.load(server.mappath("app.xml"))
end sub
private sub class_terminate()
set xmldom = nothing
end sub
function getd(key)
getd =xmldom.getelementsbytagname(key)(0).text
end function
function setd(key,val)
xmldom.getelementsbytagname(key)(0).text = val
xmldom.save(server.mappath("app.xml"))
end function
function addd(node,key,val)
set newnode=xmldom.getelementsbytagname(node)(0).appendchild(xmldom.createelement(key))
newnode.text = val
set newnode=nothing
xmldom.save(server.mappath("app.xml"))
end function
function deld(key)
on error resume next
xmldom.getelementsbytagname(key)(0).parentnode.removechild(xmldom.getelementsbytagname(key)(0))
xmldom.save(server.mappath("app.xml"))
end function
end class
set config = new appconfig
wn config.getd("version")
wn config.getd("latestversion")
wn config.getd("author")
wn config.getd("pubdate")
wn config.getd("installed")
wn config.getd("bakpath")
' 去掉相应的注释符,即可看到 [添加 / 编辑 / 删除] 节点的效果
'call config.addd("config","test","test") ' 添加节点
'call config.setd("test","test2") ' 编辑节点
'call config.deld("test") ' 删除节点
sub wn(str)
response.write(str)&"<br />"&vbcrlf
end sub
%>

不是很通吃,但某些情况下的运用足够了, 基本可以实现添加/删除/修改节点
相关文章