![]() Capt. Horatio T.P. Webb |
| |
The tree walking algorithm shown below essentially reproduces the task performed by the IE5 browser when confronted with an xml file. The code parses the xml file and places the tags with appropriate format and indentations. The central component is the treewalk sub.
This sub is recursive. It works like this:
See it work here.
Here is the code:
<html>
<body>
<table width="100%" border="2">
<tr><td bgcolor="#cccccc"><center><img src="captsm.gif" border="2" align="top"><br><font face="HELVETICA, ARIAL" size="1">Capt. Horatio T.P. Webb</center></td> <td colspan="2" valign="center" bgcolor="#cccccc"><font face="HELVETICA, ARIAL" size="5"><center><b>Walking the XML Tree
</center></td></tr></table>
<p>
<%
dim nodecount
dim level
sub treewalk(node)
nodecount=nodecount+1
select case node.nodeType
case 1 '**** print the element
response.write "<br>"
for i=1 to level
response.write " "
next
response.write "<b><"+cstr(node.nodeName)+"</b>"
if node.attributes.length > 0 then
for i=0 to (node.attributes.length-1)
response.write " <font color='#009900'><b>"
response.write " "+cstr(node.attributes.item(i).nodeName)
response.write "</font></b>=<font color='#0000cc'><b>"+chr(39)
response.write cstr(node.attributes.item(i).text)+chr(39)+"</font></b>"
next
end if
response.write "<b>></b>"
for c=0 to (node.ChildNodes.length-1) '*** walk the kids
level=level+1
call treewalk(node.childNodes(c))
level=level-1
next
if node.childNodes.length > 1 then '*** if there are really kids not just text for this node
response.write "<br>"
for i=1 to level
response.write " "
next
end if
response.write "<b></"+cstr(node.nodeName)+"></b>" '***closing tag
case 3 '**** print the node's content
response.write "<font color='#cc0000'><b>"
response.write cstr(node.text)
response.write "</b></font>"
end select
end sub
'
'
'*** top of main
'
'
if request.form("token")= "" then
%>
<form name="xmlform" action="treewalk.asp" METHOD="POST"><b>
Enter XML file to parse <input type="text" name="xfile" value="jv2.xml" size="40">
<p>
<input type="hidden" name="token" value="2">
<input type="submit" value="Walk the Tree"></b>
</form>
<%
else
fx=request.form("xfile")
set xmldoc=Server.CreateObject("Microsoft.xmldom")
response.write "<p><b>Tree Walk for file="+cstr(fx)+"<p>"
response.write "<img src='black1x1.gif' width='14' height='14' border='2'><font color='000000'> Element tags are black<br></font>"
response.write "<img src='red1x1.gif' width='14' height='14' border='2'><font color='cc0000'> Content tags are red<br></font>"
response.write "<img src='green1x1.gif' width='14' height='14' border='2'><font color='009900'> Attributes are green<br></font>"
response.write "<img src='blue1x1.gif' width='14' height='14' border='2'><font color='0000cc'> Attribute values are blue<br></font><p></b>"
xmldoc.async=false
xmldoc.load(Server.MapPath(fx))
if (xmldoc.parseError <> 0) then
response.write "<p>Parse Error Found:" + cstr(xmldoc.parseError.reason)
response.write "<p>ASP Program is terminating."
else
nodecount=0
level=0
call treewalk(xmldoc.documentElement)
response.write "<p><b>Node count="+cstr(nodecount)
response.write "<p>Click <a href='treewalk.htm'> here</a> for the treewalk.asp source code.</b>"
end if
end if
%>
</body>
</html>