ASP操纵XML类

www.u8686.com-信息发布平台

ASP操纵XML类

 

<%
class cls_xml

private sfilename
private sxmlfile
private sobjxml

property let filename(value)
sfilename=value
end property

property get filename
filename=sfilename
end property

property get objxml
set objxml=sobjxml
end property

private sub class_initialize()
sfilename=""
sxmlfile=""
sobjxml=null
end sub

private sub class_terminate()
sobjxml.close
set sobjxml=nothing
end sub

private sub createfile()'创建xml文件
Set oPI=sobjXML.createProcessingInstruction("xml", "version=""1.0"" encoding=""GB2312""")
sobjXML.insertBefore oPI,sobjXML.childNodes(0)
savefile
end sub

public function loadfile()'读取xml文件
sxmlfile=server.MapPath(sfilename)
set sobjxml=server.CreateObject("msxml2.domdocument")
sobjxml.load(sxmlfile)
sobjxml.async=false
if sobjxml.parseError.ErrorCode = 0 then
loadfile=true
else
createfile
end if
end function

public sub addnode(byval parentnode,byval childnode,byval nodetext)'添加节点
if parentnode="" then
sobjxml.appendchild (sobjxml.createElement(childnode))
else
set child=sobjxml.createElement(childnode)
child.text=nodetext
sobjxml.selectsinglenode(parentnode).appendchild child
end if
savefile
end sub

public sub addattr(byval parentnode,byval attrtype,byval attrname,byval attrtext)'添加属性
set sattribute=sobjxml.createNode(attrtype,attrname,"")
sattribute.text=attrtext
if parentnode="" then
sobjxml.documentElement.setattributenode sattribute
else
sobjxml.documentElement.selectsinglenode(parentnode).setattributenode sattribute
end if
savefile
end sub

private function getnode(byval nodepath,byval attrname,byval attrvalue)'获得节点对象
if attrname="" then
set getnode=sobjxml.documentElement.selectsinglenode(nodepath)
else
set getnode=sobjxml.documentElement.selectsinglenode("//"&nodepath&"[@"&attrname&"='"&attrvalue&"']")
end if
end function

public function getnodename(byval nodepath,byval attrname,byval attrvalue)'获得节点名称
getnodename=getnode(nodepath,attrname,attrvalue).nodename
end function

public function getnodevalue(byval nodepath,byval attrname,byval attrvalue)'获得节点内容
getnodevalue=getnode(nodepath,attrname,attrvalue).text
end function

public sub modifynodevalue(byval nodepath,byval newtext,byval attrname,byval attrvalue)'修改节点内容
set newnode=getnode(nodepath,attrname,attrvalue)
newnode.text=newtext
savefile
end sub

public function getnodeattrname(byval nodepath,byval attrname,byval attrname_,byval attrvalue_)'获得节点属性名称
getnodeattrname=getnode(nodepath,attrname_,attrvalue_).getattributenode(attrname).nodename
end function

public function getnodeattrvalue(byval nodepath,byval attrname,byval attrname_,byval attrvalue_)'获得节点属性值
getnodeattrvalue=getnode(nodepath,attrname_,attrvalue_).getattributenode(attrname).nodevalue
end function

public sub modifynodeattrvalue(byval nodepath,byval attrname,byval newtext,byval attrname_,byval attrvalue_)'修改节点属性值
set newattr=getnode(nodepath,attrname_,attrvalue_).getattributenode(attrname)
newattr.text=newtext
savefile
end sub

public sub delnode(byval parentnodepath,byval parentnodeattrname,byval parentnodeattrvalue,byval childnodepath,byval childnodeattrname,byval childnodeattrvalue)'删除节点
if parentnodepath="" then
set parentnode=sobjxml.documentElement
else
set parentnode=getnode(parentnodepath,parentnodeattrname,parentnodeattrvalue)
end if
set childnode=getnode(childnodepath,childnodeattrname,childnodeattrvalue)
parentnode.removechild childnode
savefile
end sub

public function getchildlength(byval nodename,byval attrname,byval attrvalue)'获得节点长度
if nodename="" then
getchildlength=sobjxml.documentElement.childnodes.length
else
getchildlength=getnode(nodename,attrname,attrvalue).childnodes.length
end if
end function

private sub savefile()'保存文件
sobjxml.save(sxmlfile)
end sub

end class
%>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值