Dom遍历XML的一个例子,结果为树状结构

knowledge.XML文件

 1<ebaihe id="gs000" name="Ebaihe" type="gs">   
 2&lt;知识管理 name="知识管理" type="rt" id="rt000"&gt;   
 3&lt;目录 name="vc++" type="ml" id="ml003"/&gt;   
 4&lt;目录 name="asp" type="ml" id="ml001"&gt;   
 5&lt;知识 name="什么是ASP 1" type="zs" id="zs001"&gt;   
 6<title>什么是ASP</title>
 7<author>cxx</author>
 8<adddate>2001-02-20</adddate>
 9<content>ASP:Active Server Page</content>
10<!--知识-->   
11&lt;知识 name="什么是ASP 2" type="zs" id="zs002"&gt;   
12<title>什么是ASP</title>
13<author>cxx</author>
14<adddate>2001-02-20</adddate>
15<content>ASP:Active Server Page</content>
16<!--知识-->
17<!--目录-->   
18&lt;目录 name="vbs" type="ml" id="ml002"&gt;   
19&lt;知识 name="什么是vbs 1" type="zs" id="zs003"&gt;   
20<title>什么是ASP</title>
21<author>cxx</author>
22<adddate>2001-02-20</adddate>
23<content>ASP:Active Server Page</content>
24<!--知识-->   
25&lt;知识 name="什么是vbs 2" type="zs" id="zs004"&gt;   
26<title>什么是ASP</title>
27<author>cxx</author>
28<adddate>2001-02-20</adddate>
29<content>ASP:Active Server Page</content>
30<!--知识-->
31<!--目录-->
32<!--知识管理-->   
33&lt;留言簿 name="留言簿" type="ly" id="ly000"&gt;   
34&lt;单元 name="你好" type="dy" id="dy001"&gt;   
35&lt;留言 name="Who are you" type="zs" id="zs005"&gt;   
36<title>什么是ASP</title>
37<author>cxx</author>
38<adddate>2001-02-20</adddate>
39<content>ASP:Active Server Page</content>
40<!--留言-->   
41&lt;留言 name="你好" type="zs" id="zs006"&gt;   
42<title>什么是ASP</title>
43<author>cxx</author>
44<adddate>2001-02-20</adddate>
45<content>ASP:Active Server Page</content>
46<!--留言-->
47<!--单元-->   
48&lt;单元 name="我的留言" type="dy" id="dy003"/&gt;   
49<!--留言簿-->   
50&lt;留言簿 name="留言簿" type="ly" id="ly001"/&gt;   
51</ebaihe>

bianli.asp文件

1 @LANGUAGE=VBScript
1   
2function getimgstr(ttid,ttname,ttlvl,tttype,ttlf,ttlstr)   
3dim ttstr1   
4if ttlvl=0 then   
5ttstr1=ttstr1 & "

<a "="" "')"="" "','"="" "2"="" #"="" &="" call="" chr(34)="" href=" &amp; chr(34) &amp; " onclick=" &amp; chr(34) &amp; " onclickdiv('"="" ttid="" ttstr1="ttstr1">"
ttstr1= ttstr1 &amp; "<img "="" &="" 0"="" border=" &amp; chr(34) &amp; " chr(34)="" computer.gif"="" height="16" image="" src=" &amp; chr(34)
ttstr1= ttstr1 &amp; "/>" &amp; "</a>

1"   
2ttstr1=ttstr1 & "

<a "="" "')"="" "','"="" "2"="" #"="" &="" call="" chr(34)="" href=" &amp; chr(34) &amp; " onclick=" &amp; chr(34) &amp; " onclickdiv('"="" ttid="" ttstr1="ttstr1">"
ttstr1=ttstr1 &amp; ttname &amp; "</a>

<br/>

1" & vbcrlf   
2ttstr1=ttstr1 & "

<div id=" &amp; chr(34) &amp; ttid &amp; chr(34) &amp; " style="display:none">" &amp; vbcrlf
else
for i= 2 to len(ttlstr)-1
if mid(ttlstr,i,1)="1" then
ttstr1 = ttstr1 &amp; "<img "="" &="" 0"="" border=" &amp; chr(34) &amp; " chr(34)="" height="16" i-line.gif"="" image="" src=" &amp; chr(34)
ttstr1 = ttstr1 &amp; "/>"
end if
if mid(ttlstr,i,1)="0" then
ttstr1 = ttstr1 &amp; "<img "="" &="" 0"="" border=" &amp; chr(34) &amp; " chr(34)="" height="16" image="" none.gif"="" src=" &amp; chr(34)
ttstr1 = ttstr1 &amp; "/>"
end if
next
if tttype=1 then
ttstr1= ttstr1 &amp; "<a "="" "')"="" "','"="" #"="" &="" call="" chr(34)="" href=" &amp; chr(34) &amp; " onclick=" &amp; chr(34) &amp; " onclickdiv('"="" ttid="" ttlf="" ttstr1="ttstr1">"
ttstr1=ttstr1 &amp; "<img "="" &="" 0"="" border=" &amp; chr(34) &amp; " chr(34)="" height="16" id=" &amp; chr(34) &amp; " image="" l-"="" src=" &amp; chr(34)
if ttlf=0 then
ttstr1= ttstr1 &amp; " t-plus.gif"="" ttid=""/></a>"
else
ttstr1= ttstr1 &amp; "image/l-plus.gif" &amp; chr(34) &amp; " id=" &amp; chr(34) &amp; "l-" &amp; ttid &amp; chr(34) &amp; "
height=16&gt;"
end if
ttstr1= ttstr1 &amp; "<a "="" "')"="" "','"="" #"="" &="" call="" chr(34)="" href=" &amp; chr(34) &amp; " onclick=" &amp; chr(34) &amp; " onclickdiv('"="" ttid="" ttlf="" ttstr1="ttstr1">"
ttstr1=ttstr1 &amp; "<img "="" &="" 0"="" border=" &amp; chr(34) &amp; " c-fold.bmp"="" chr(34)="" height="16" id=" &amp; chr(34) &amp; " image="" p-"="" src=" &amp; chr(34)
ttstr1= ttstr1 &amp; " ttid=""/></a>"
ttstr1= ttstr1 &amp; "<a "="" "')"="" "','"="" #"="" &="" call="" chr(34)="" href=" &amp; chr(34) &amp; " onclick=" &amp;chr(34) &amp; " onclickdiv('"="" ttid="" ttlf="" ttstr1="ttstr1">"
ttstr1=ttstr1 &amp; ttname &amp; "</a><br/>" &amp; vbcrlf
ttstr1=ttstr1 &amp; "<div id=" &amp; chr(34) &amp; ttid &amp; chr(34) &amp; " style="display:none">" &amp; vbcrlf
else
ttstr1 = ttstr1 &amp; "<img "="" &="" 0"="" border=" &amp; chr(34) &amp; " chr(34)="" height="16" image="" src=" &amp; chr(34)
if ttlf=0 then
ttstr1 = ttstr1 &amp; " t-line.gif"=""/>"
else
ttstr1 = ttstr1 &amp; "image/l-line.gif" &amp; chr(34) &amp; " height=16&gt;"
end if
ttstr1= ttstr1 &amp; "<a "="" "','3')"="" #"="" &="" call="" chr(34)="" href=" &amp; chr(34) &amp; " onclick=" &amp; chr(34) &amp; " onclickdiv('"="" ttid="" ttstr1="ttstr1">"
ttstr1=ttstr1 &amp; "<img "="" &="" 0"="" border=" &amp; chr(34) &amp; " chr(34)="" height="16" id=" &amp; chr(34) &amp; " image="" leaf.bmp="" p-"="" src=" &amp; chr(34)
ttstr1= ttstr1 &amp; " ttid=""/></a>"
ttstr1= ttstr1 &amp; "<a "="" "','3')"="" #"="" &="" call="" chr(34)="" href=" &amp; chr(34) &amp; " onclick=" &amp; chr(34) &amp; " onclickdiv('"="" ttid="" ttstr1="ttstr1">"
ttstr1=ttstr1 &amp; ttname &amp; "</a><br/>" &amp; vbcrlf
ttstr1=ttstr1 &amp; "<div id=" &amp; chr(34) &amp; ttid &amp; chr(34) &amp; " style="display:none"></div>" &amp; vbcrlf
end if
end if
getimgstr=ttstr1
end function
Function ShowNode(node,mylvl,myleaf,mylstr)
dim ttstr
dim mystr
dim ttleaf
dim ttn
ttn=0
if myleaf=0 then
ttleaf=0
else
ttleaf=1
end if
For Each child In node.childNodes
ttn=ttn+1
If (child.nodeType=1) Then
nod_name=child.Attributes.getnamedItem("name").Text
nod_type=child.Attributes.getnamedItem("type").Text
nod_id=child.Attributes.getnamedItem("id").Text
if nod_type&lt;&gt;"zs" then
If (child.hasChildNodes) Then
if ttn=node.childnodes.length then
mystr=mystr &amp; getimgstr(nod_id,nod_name,mylvl,1,1,mylstr &amp; "0")
ttstr=ShowNode(child,mylvl+1,1,mylstr &amp; "0")
else
mystr=mystr &amp; getimgstr(nod_id,nod_name,mylvl,1,0,mylstr &amp; "1")
ttstr=ShowNode(child,mylvl+1,0,mylstr &amp; "1")
end if
mystr=mystr &amp; ttstr
mystr=mystr &amp; "</div>"
else
if ttn=node.childnodes.length then
mystr=mystr &amp; getimgstr(nod_id,nod_name,mylvl,0,1,mylstr &amp; "0")
else
mystr=mystr &amp; getimgstr(nod_id,nod_name,mylvl,0,0,mylstr &amp; "1")
end if
End If
else
if ttn=node.childnodes.length then
mystr=mystr &amp; getimgstr(nod_id,nod_name,mylvl,0,1,mylstr &amp; "0")
else
mystr=mystr &amp; getimgstr(nod_id,nod_name,mylvl,0,0,mylstr &amp; "1")
end if
end if
end if
Next
ShowNode=mystr
End Function
dim xmldoc
dim xmlstr
Set xmldoc = Server.CreateObject("Microsoft.XMLDOM")
xmldoc.async=false
xmldoc.load(Server.MapPath("knowledge.xml"))
xmlstr=ShowNode(xmldoc,0,0,"")
' set xmldoc=nothing
'parent.results.display(source.transformNode(style));

 1<html>
 2<head>
 3</head>
 4<body>
 5<script language="VBScript">   
 6  
 7dim olditem   
 8  
 9sub OnClickDiv(DivId,pType)   
10if (document.all(DivId).style.display="none") then   
11document.all(DivId).style.display=""   
12if (pType="0") then   
13document.all("l-"+DivId).src="image/t-minus.gif"   
14document.all("p-"+DivId).src="image/o-fold.bmp"   
15end if   
16if (pType="1") then   
17document.all("l-"+DivId).src="image/l-minus.gif"   
18document.all("p-"+DivId).src="image/o-fold.bmp"   
19end if   
20'if (pType="3") then   
21' document.all("p-"+DivId).src="image/o-fold.bmp"   
22'end if   
23else   
24document.all(DivId).style.display="none"   
25if (pType="0") then   
26document.all("l-"+DivId).src="image/t-plus.gif"   
27document.all("p-"+DivId).src="image/c-fold.bmp"   
28end if   
29if (pType="1") then   
30document.all("l-"+DivId).src="image/l-plus.gif"   
31document.all("p-"+DivId).src="image/c-fold.bmp"   
32end if   
33'if (pType="3") then   
34' document.all("p-"+DivId).src="image/c-fold.bmp"   
35'end if   
36end if   
37end sub   
38</script>
39<font name="宋体" size="2">
40<nobr>```
41=xmlstr
42```</nobr></font>
43</body></html></div>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus