首页 >> 下载中心 >> Asp源码 >> asp操纵xml类源代码

asp操纵xml类源代码

作者: 来源:一天到晚游泳的鱼 时间:2008-5-8 网友评论条 【

<%
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
%>

站长工具
Google 相关搜索查询:
相关文章
loading 请稍等,评论加载中...

Aspxhome.com. 中国Asp之家. 版权所有

闽ICP备06017341号