兔子专栏

刘晓逸(liuxiaoyi666)的小地盘

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

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭