ASP操作XML的类

发布时间:2010年03月16日      浏览次数:369 次
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
测试ASP处理XML数据<br>
<%
'test.xml文件中的数据
'<?xml version="1.0" encoding="utf-8"?>
'<xml>
'      <web>
'            <items><id>1</id><txt>网站建设</txt></items>
'            <items><id>2</id><txt>优化推广</txt></items>
'            <items><id>3</id><txt>技术支持</txt></items>
'            <items><id>4</id><txt>友情链接</txt></items>
'            <items><id>5</id><txt>广告服务</txt></items>
'            <items><id>6</id><txt>域名空间</txt></items>
'      </web>
'</xml>
Response.Charset="utf-8"
'实例-----------------------------------------
dim xml,xmlfile
xmlfile="test.xml"
set xml=new QT_XML_Class
'call xml.joinxml("/xml/web","<items><id>2</id><txt>新添加的节点</txt></items>")
response.Write (xml.f_node("web")) '显示此节点的内容
response.End()
'---------------------------------------------
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&"<xml>"&vbcrlf&"<web>"&vbcrlf&"<items><id>1</id><txt>网站建设</txt></items>"&vbcrlf&"</web>"&vbcrlf&"</xml>",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
      
      '生成XML文件
      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
'-----
'-----添加
'-----
      
      '判断节点结构,指定true 则在不存在时新建。 如:xml.checknode("/blog/b/c/e",true) ,根据xml.iserr_ 为True 则存在, 为False 则不存在
      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
      
      '将字符作为子节点,插入指定位置。 如:call xml.joinxml("/blog/sorts","<sort><id>3</id><name>分类3</name><total>33</total></sort>")
      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
      
      '如果节点数 < 指定数 则删除first节点,然后在最后新加一个同级节点。 如:call xml.add_node("/blog/sorts/sort","<sort><id>3</id><name>分类3</name><total>33</total></sort>",3)
      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
'-----
'-----修改
'-----
      
      '将第一条件值替换成新值。 如:call xml.r_node("/blog/sorts/sort[id='2']/name","新值")
      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
      
      '将全部条件值替换成新值。 如:call xml.r_nodes("/blog/sorts/sort[id='2']/name","新值")
      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
      
'-----
'-----删除
'-----
      
      '删除第一个符合条件的节点。 如:call xml.d_node("/blog/sorts/sort[id='3']")
      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
      
      '删除所有符合条件的节点()。 如:call xml.d_nodes("/blog/sorts/sort[id='2']")
      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
      
      '清空第一个符合条件的节点。 如:xml.c_node("/blog/sorts/sort[id='2']")
      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
      
      '清空全部符合条件的节点。 如:xml.c_nodes("/blog/sorts/sort[id='2']")
      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
      
      '将指点节点替换成新节点。 如:call xml.replace_node("/blog/sorts/sort[id='2']","<sort><id>新2</id><name>新分类2</name><total>新33</total></sort>")
      function replace_node(node,newstr)
            call add_node(node,newstr,0)
      End Function
      
      '查找节点。 如:call xml.f_node("/blog/sorts/sort[id='2']/name")
      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
      
      '统计节点。 如:nodes=xml.count("/blog/sorts/sort[id='2']") ,nodes 返回数组( 0 同级节点数,1 子节点数,2子集)
      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
'-------------------------------------------使用说明--开始
'----获取类的执行情况
'response.write xml.iserr_
'-----
'-----查找
'-----
'call xml.f_node("/blog/sorts/sort[id='2']/name")
'nodes=xml.count("/blog/sorts/sort[id='2']")
'nodes 返回数组( 0 同级节点数,1 子节点数,2子集)
'-----
'-----添加
'-----
'判断节点结构,指定true 则在不存在时新建。
'xml.checknode("/blog/b/c/e",true)
'根据xml.iserr_ 为True 则存在, 为False 则不存在
'将字符作为子节点,插入指定位置。
'call xml.joinxml("/blog/sorts","<sort><id>3</id><name>分类3</name><total>33</total></sort>")
'如果节点数 < 指定数 则删除first节点,然后在最后新加一个同级节点
'call xml.add_node("/blog/sorts/sort","<sort><id>3</id><name>分类3</name><total>33</total></sort>",3)
'
'-----
'-----修改
'-----
'将第一条件值替换成新值
'call xml.r_node("/blog/sorts/sort[id='2']/name","新值")
'将全部条件值替换成新值
'call xml.r_nodes("/blog/sorts/sort[id='2']/name","新值")
'将指点节点替换成新节点
'call xml.replace_node("/blog/sorts/sort[id='2']","<sort><id>新2</id><name>新分类2</name><total>新33</total></sort>")
'-----
'-----删除
'-----
'删除第一个符合条件的节点
'call xml.d_node("/blog/sorts/sort[id='3']")
'删除所有符合条件的节点()
'call xml.d_nodes("/blog/sorts/sort[id='2']")
'删除节点下的全部子节点
'call xml.d_nodes("/blog/sorts/sort[id='2']")
'-----
'-----清空
'-----
'清空第一个符合条件的节点
' xml.c_node("/blog/sorts/sort[id='2']")
'清空全部符合条件的节点
' xml.c_nodes("/blog/sorts/sort[id='2']")
'-------------------------------------------使用说明--结束
%>
免责声明:本站相关技术文章信息部分来自网络,目的主要是传播更多信息,如果您认为本站的某些信息侵犯了您的版权,请与我们联系,我们会即时妥善的处理,谢谢合作!