zl程序教程

您现在的位置是:首页 >  移动开发

当前栏目

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

ASPXML代码 操作 示例 精炼
2023-06-13 09:14:18 时间
以下保存成App.xml,与asp文件放在相同目录下!
复制代码代码如下:

<?xmlversion="1.0"encoding="utf-8"?>
<Root>
<About>
<Version>1.0Beta</Version>
<LatestVersion>1.0Beta</LatestVersion>
<Author>Author</Author>
<PubDate>2010/02/20</PubDate>
</About>
<Config>
<Installed>False</Installed>
<BakPath>_Data</BakPath>
</Config>
</Root>

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

<%
ClassAppConfig
DimXmlDom
PrivateSubClass_Initialize()
SetXmlDom=Server.createobject("microsoft.xmldom")
XmlDom.load(Server.mappath("App.xml"))
EndSub
PrivateSubClass_Terminate()
SetXmlDom=Nothing
EndSub
FunctionGetD(key)
GetD=XmlDom.getElementsByTagName(key)(0).text
EndFunction
FunctionSetD(key,val)
XmlDom.getElementsByTagName(key)(0).text=val
XmlDom.save(Server.mappath("App.xml"))
EndFunction
FunctionAddD(node,key,val)
Setnewnode=XmlDom.getElementsByTagName(node)(0).appendchild(XmlDom.createelement(key))
newnode.text=val
Setnewnode=Nothing
XmlDom.save(Server.mappath("App.xml"))
EndFunction
FunctionDelD(key)
OnErrorResumeNext
XmlDom.getElementsByTagName(key)(0).parentNode.removechild(XmlDom.getElementsByTagName(key)(0))
XmlDom.save(Server.mappath("App.xml"))
EndFunction
EndClass
SetConfig=newAppConfig
wnConfig.GetD("Version")
wnConfig.GetD("LatestVersion")
wnConfig.GetD("Author")
wnConfig.GetD("PubDate")
wnConfig.GetD("Installed")
wnConfig.GetD("BakPath")
"去掉相应的注释符,即可看到[添加/编辑/删除]节点的效果
"CallConfig.AddD("Config","test","test")"添加节点
"CallConfig.SetD("test","test2")"编辑节点
"CallConfig.DelD("test")"删除节点
Subwn(str)
Response.Write(str)&"<br/>"&vbcrlf
EndSub
%>

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