Capt. Horatio T.P. Webb
Walking the XML Tree

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:

  1. first an xml file is loaded
  2. then the sub treewalk is passed the root of the xml document
  3. if an element is found the code:
    1. prints the nodeName
    2. checks for attributes (node.attributes.length)
      if found (node.attributes.length > 0), it loops through the attributes and writes
      the attribute nodeName in green and the attribute's text value in blue
    3. checks for childNodes
      if children are found, the sub calls itself (this is the recursive part) to walk down the tree for each childNode
      if only one child is found, the sub terminates the tag on the same line
          (Note: the tag's content has already been printed by the call to treewalk)
      if more than one child was found, the sub starts a new line, and indents the ending tag
  4. if the sub finds a text node it just prints the value in red text. This is the case where a node has only one child.

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>