网站首页 手机版
 注册 登录
您现在的位置: 畅无忧设计 >> 网络编程 >> ASP教程 >> 正文
最新文章
· FSO 组件asp生成html静态页面碰到缓存
· asp实现长文章自动分页插件
· 在ASP中访问和更新Cookies集合
· ASP错误提示大全
· 学习ASP的几个观点
· ASP用两级联动下拉列表来显示大类和小
· ASP取当前页面地址和参数
· ASP删除记录的同时删除相关图片
· asp将查询结果导出到excel
· ASP批量导入Excel到Access或者Sql Se
热门文章
 化境ASP无组件上传类 - upload_5xs
 一个获取ACCESS数据库表名以及表名
 asp将查询结果导出到excel
 艾恩ASP无组件上传修改版
 ASP批量导入Excel到Access或者Sql 
 ASP读取数据库的Flash+JS图片切换特
 ASP用两级联动下拉列表来显示大类和
 ASP+JS实现网页歌曲连播、点播功能
 使用ASP重启服务器
 asp批量替换access数据库中指定字段
相关文章
没有相关文章
ASP类对XML进行操作
来源:中国站长天空 更新时间:2009/5/14 16:36:19 阅读次数:
字体:[ ] 我要投稿

说明:
可以完成ASP对XML节点的添加、删除、修改、清空。
进行上述操作,根据条件的不同,操作类型包括二种:仅对第一个符合条件的值进行操作、及全部符合条件的值进行操作。

例如:
将第一条件值替换成新值:
call xml.r_node("/blog/sorts/sort[id='1']/name","分类1")
将全部条件值替换成新值:
call xml.r_nodes("/blog/sorts/sort[id='1']/name","分类1")

完整的代码如下:
<%dim xml,xmlfile
xmlfile="/blog/1/1/blog.xml"
set xml=new QT_XML_Class
Class QT_XML_Class
private dom,xmlpath,doc
Public iserr_
Private Sub Class_Initialize()
Set dom = Server.CreateObject("Microsoft.FreeThreadedXMLDOM")
xmlpath = Server.MapPath(xmlfile)
If not dom.Load(xmlpath) Then
SaveToFile "<?xml version=""1.0"" encoding=""utf-8""?>"&vbcrlf&"<blog>"&vbcrlf&"<sorts>"&vbcrlf&"</sorts>"&vbcrlf&"<blogs>"&vbcrlf&"</blogs>"&vbcrlf&"<re>"&vbcrlf&"</re>"&vbcrlf&"</blog>",xmlpath 
dom.Load(xmlpath)
End If
end Sub
Private Sub Class_Terminate
If IsObject(dom) Then Set dom = Nothing
If IsObject(doc) Then Set doc = Nothing
End Sub
Private Function SaveToFile(ByVal strBody,ByVal SavePath)
dim ado
Set ado = Server.CreateObject("ADODB.Stream")
ado.Open
ado.Type = 2
ado.charset = "utf-8"
ado.WriteText strBody
ado.SaveToFile SavePath,2
ado.Close
Set ado = Nothing
End Function
function d_node(node)
iserr_=False
set doc=dom.documentElement.selectSingleNode(node)
if not doc is nothing then
doc.parentNode.removeChild(doc)
dom.save(xmlpath)
iserr_=True
end if
set doc=nothing
End Function
function d_nodes(node)
dim i
iserr_=False
set doc=dom.selectNodes(node)
if not doc is nothing then
for i=0 to doc.length-1
doc.item(i).parentNode.removeChild(doc.item(i))
next
iserr_=True
end if
dom.save(xmlpath)
set doc=nothing
End Function
function c_node(node)
iserr_=False
set doc=dom.documentElement.selectSingleNode(node)
if not doc is nothing then
doc.text=""
dom.Save(xmlpath)
iserr_=True
end if
set doc=nothing
end function
function c_nodes(node)
dim i
iserr_=False
set doc=dom.selectNodes(node)
if not doc is nothing then
for i=0 to doc.length-1
doc.item(i).text=""
next
dom.Save(xmlpath)
iserr_=True
end if
set doc=nothing
end function
function checknode(nodes,build)
dim doc2,doc3
dim i,f_node_,n_node,newnode
iserr_=True
Set doc = dom.documentElement.selectSingleNode(nodes)
if doc is nothing then
iserr_=False
if build then
nodes=split(nodes,"/")
f_node_=""
n_node=""
for i=0 to ubound(nodes)-1
if nodes(i)="" then
f_node_=f_node_&nodes(i)
f_node_=f_node_&"/"
n_node=f_node_&nodes(i+1)
else
f_node_=n_node
n_node=f_node_&"/"&nodes(i+1)
end if
Set doc2 = dom.documentElement.selectSingleNode(f_node_)
set doc3 = dom.documentElement.selectSingleNode(n_node)
if doc3 is nothing then
Set newnode = dom.createElement(nodes(i+1))
newnode.Text=""
doc2.AppendChild(newnode)
Set newnode=nothing
end if
set doc2=nothing
set doc3=nothing
next
dom.Save(xmlpath)
end if
end if
set doc=nothing
End Function
function joinxml(inset_node,xmlstr)
dim oldxml,newxml,rootNewNode
iserr_=False
Set oldXML = Server.CreateObject("Microsoft.XMLDOM")
oldXML.load(xmlpath)
set doc=oldxml.documentElement.selectSingleNode(inset_node)
if not doc is nothing then
iserr_=True
Set newXML = Server.CreateObject("Microsoft.XMLDOM")
newXML.loadXML(xmlstr&vbcrlf)
set rootNewNode=newXML.documentElement
doc.appendChild(rootNewNode)
oldxml.Save(xmlpath)
end if
set oldXML=nothing
set newXML=nothing
End Function
function r_node(node,newstr)
iserr_=False
set doc=dom.documentElement.selectSingleNode(node)
if not doc is nothing then
doc.text=newstr
iserr_=True
end if
set doc=nothing
dom.Save(xmlpath)
End Function
function r_nodes(node,newstr)
dim i
iserr_=False
set doc=dom.selectNodes(node)
if not doc is nothing then
for i=0 to doc.length-1
doc.item(i).text=newstr
next
iserr_=True
end if
set doc=nothing
dom.Save(xmlpath)
End Function
function replace_node(node,newstr)
call add_node(node,newstr,0)
End Function
function add_node(node,newstr,num)
set doc=dom.selectNodes(node)
if not doc is nothing then
if doc.length >=num then
call d_node(node)
end if
end if
set doc=nothing
call joinxml(left(node,instrrev(node,"/")-1),newstr)
End Function
function f_node(node)
dim getnode
set doc=dom.documentElement.selectSingleNode(node)
if not doc is nothing then
iserr_=True
getnode=doc.Text
else
getnode=""
iserr_=False
end if
set doc=nothing
f_node=getnode
end function
function count(node)
dim nodenum
nodenum=array(0,0,"")
iserr_=False
set doc=dom.selectNodes(node)
if not doc is nothing then
nodenum(0)=doc.length
nodenum(2)=doc.item(0).xml
if doc.item(0).hasChildNodes() then
nodenum(1)=doc.item(0).childNodes.length
end if
iserr_=True
end if
count=nodenum
end function
function id_(str)
id_=String(10-len(str),"0")
end function
function now_(dd)
dim m,d,h,mm
if not isdate(dd) then d=now()
dd=cdate(dd)
m=month(dd)
d=day(dd)
h=hour(dd)
mm=Minute(dd)
if m<10 then m="0"&m
if d<10 then d="0"&d
if h<10 then h="0"&h
if mm<10 then mm="0"&mm
now_=year(dd)&"-"&m&"-"&d&" "&h&":"&mm&":"&Second(dd)
end function
end class%>

  • 上一篇文章:
  • 下一篇文章:
  • 关于我们 - 联系我们 - 广告服务 - 在线投稿 - 友情链接 - 网站地图 - 版权声明
    CopyRight 2008-2010, CWYDESIGN.COM - 畅无忧设计, Inc. All Rights Reserved
    滇ICP备09005765号