The Walktree Function
The Walktree Function
Waltree is a simple VBScript function I devised while I was teaching myself rudimentary use of Microsoft’s XML Dom. The function takes a XML DOMDocument object as an argument and will output the XML as an indented, unordered HTML list. Walktree will not only display the content of each XML tag but also the values of the various properties if they exist.
Specifically this Function illustrates the usage of:
- Microsoft’s XML DOM implementation
- Simple Recursion
Calling the function is not difficult. You start by creating a XML object like so:
Dim xmlDoc
Set xmlDoc = CreateObject("Msxml2.DOMDocument")
Then set the appropriate XML properties and load the document you wish to porcess.
xmlDoc.async = False
xmlDoc.load(Server.MapPath("books.xml"))
Finally check if there were errors and if not run the Walktree function.
If (xmlDoc.parseError.errorCode <> 0) Then
response.Write(xmlDoc.parseError.reason & "<BR>")
Else
Walktree(xmlDoc)
End If
Walktree Source
The source below should be fairly self-explanatory if you’re familiar with VBScript. The output of the function can be easily modified to cater to other needs.
<%
Function WalkTree(objCurrNode)
’====================================================================
’ WALKTREE 1.0
’ AUTHOR: Thomas Vanparys - http://www.badsoda.com
’
’ USAGE: Pass a valid XML DOMDocument object or node and it will
’ output the entire child tree as an HTML unordered list
’====================================================================
Dim objChild
Dim objAttributes
‘Check if node passed down has children
If objCurrNode.hasChildNodes = True then
response.Write("<ul>")
‘Output each child
For Each objChild In objCurrNode.childNodes
‘While making sure they are elements and not something else
If objChild.nodeTypeString = "element" then
response.Write("<li>")
response.Write(objChild.tagName)
‘Start checking for attributes
Set objAttributes = objChild.attributes
If Not objAttributes.length = 0 then
response.write(" <i>(Attributes: ")
For i=0 To (objAttributes.length -1)
If Not i=0 then response.write("; ")
response.write(objAttributes.Item(i).nodeName)
response.write("=" & Chr(34)) response.write(objAttributes.Item(i).nodeValue)
response.write(Chr(34))
Next
response.write(")</i>")
End If
‘Output contents of current object
If objChild.hasChildNodes = True Then
‘and objChild.firstChild.nodeTypeString = "text" Then
response.Write(": " & objChild.firstChild.nodeValue)
End If
response.Write("</li>")
End If
‘Do some recursion to walk down the entire tree
Walktree(objChild)
Next
response.Write("</ul>")
End If
End Function
%>
You’re currently reading “The Walktree Function”, an entry on BadSoda
- Published:
- 08.08.03 / 1pm
- Category:
- lab
- Tags:
- Geeky
- Post Navigation:
- « Protecting your Image(ry)
Protecting your Image(ry) »




















