网络编程
位置:首页>> 网络编程>> Asp编程>> Asp操作Xml的精炼类,含示例代码

Asp操作Xml的精炼类,含示例代码

 来源:asp之家 发布时间:2011-02-28 11:11:00 

标签: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
%>


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

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com