用vbs实现xml的树转二叉树

思想来自于,二叉树的链表,因为vbs里没有,所以用dictionary去模拟的,每个dictionary都有一个

leftnode和一个rightnode,这样就可以很好的把xml的值放到里边了,并用了递归的方法 

代码如下:

<script language='vbscript'>
set objxml=createobject("msxml2.DOMDocument")
objxml.load "c:/bloginfo.xml"
objxml.async=false
set xmlroot=objxml.documentElement
set dicadd=createobject("scripting.dictionary")
dicadd.add "leftnode",recursion(xmlroot)
function recursion(theelement)
  if theelement.childnodes(0).childnodes.length<>0 then
   if theelement.nodename<>"#text" then
       set dicxml_0=createobject("scripting.dictionary")
       for i=0 to theelement.childnodes.length-1
            set info=createobject("scripting.dictionary")
     info.add "nodename",theelement.childnodes(i).text
           if not (i+1)>theelement.childnodes.length-1 then
          str="set dicxml_" & i+1 & "=createobject(" & """" & "scripting.dictionary" & """" & ")"
      execute(str)
      set xmldom=theelement.childnodes(i)
      str="dicxml_" & i & ".add " & """" & "leftnode" & """" & ",recursion(xmldom)"
      execute(str)
      str="dicxml_" & i & ".add " & """" & "rightnode" & """" & ",dicxml_" & i+1
      execute(str)
      str="dicxml_" & i & ".add " & """" & "info" & """" & ",info"
      execute(str)     
    else
            str="dicxml_" & i & ".add " & """" & "rightnode" & """" & "," & """" & "null" & """"
     execute(str)
     str="dicxml_" & i & ".add " & """" & "leftnode" & """" & "," & """" & theelement.text & """"
     execute(str)
   end if
    set info=nothing
       next
       set recursion=dicxml_0
      else
           recursion=theelement.text
      end if
    else
       recursion=null
    end if
end function
</script>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值