加入收藏 | 设为首页 | 会员中心 | 我要投稿 银川站长网 (https://www.0951zz.com/)- 云通信、基础存储、云上网络、机器学习、视觉智能!
当前位置: 首页 > 站长学院 > Asp教程 > 正文

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

发布时间:2023-07-03 13:30:15 所属栏目:Asp教程 来源:
导读:以下保存成 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><Pu

以下保存成 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

%>

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

(编辑:银川站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章