troubleshooting Question

Help with traversing xml with Microsoft.XMLDOM (non xslt)

Avatar of greddin
greddinFlag for United States of America asked on
ASPXML
12 Comments1 Solution362 ViewsLast Modified:
I would like to know how to traverse an xml file using the Microsoft.XMLDOM and ASP only and non-xslt.  I have ncluded a sample of my xml file below for you to see.  I also have my code that I have so far.

When the page loads I'd like to expand the current selected section. The current selected section is determined by a query string "id" variable.

 For example, here's a sample output for what the "Products" page menu would be. If the query string id=4. (Products).

Products
    TVs
    Stereos
    DVRs
    CD Players
Services
About Us

If the user clicks on the "Services" page then the menu's sample output would be (id = 12).

Products
Services
    Installation
    Repair
About Us

And if the user clicked on About Us the sample output would be (id = 15):

Products
Services
About Us
    Employment
    Contact Us

(Clicking Employment the output would be (id=16):

Products
Services
About Us
    Employment
        Upload Resume
        Job Openings
    Contact Us

I'm just trying to show that I would always be giving an id to the code so that it knows which section to be "selected".

======== XML File =======================
<?xml version="1.0" encoding="utf-8"?>
<site href="index.htm" id="2" label="Home" level="0">
  <section href="Products/index.htm" id="3" label="Products" level="1">
    <section href="TVs/index.html" id="4" label="TVs" level="2"/>
    <section href="Stereos/index.html" id="5" label="Stereos" level="2"/>
    <section href="DVRs/index.html" id="6" label="DVRs" level="2"/>
    <section href="CDPlayers/index.html" id="10" label="CD Players" level="2"/>
  </section>
  <section href="Services/index.htm" id="12" label="Services" level="1">
    <section href="Installation/index.htm" id="13" label="Installation" level="2"/>
    <section href="Repair/index.htm" id="14" label="Repair" level="2"/>
  </section>
  <section href="AboutUs/index.htm" id="15" label="About Us" level="1">
    <section href="Employment/index.htm" id="16" label="Employment" level="2">
      <section href="Resume/index.htm" id="17" label="Upload Resume" level="3"/>
      <section href="Jobs/index.htm" id="18" label="Job Openings" level="3"/>
    </section>
    <section href="ContactUs/index.htm" id="19" label="Contact Us" level="2"/>
  </section>
</site>

The problem with this code below is that is displays too much. It displays even the root node.
======= my code so far ===============================
<%
Dim xmlDoc
Set xmlDoc = Server.CreateObject("Microsoft.XMLDOM")

sub loadXML()
    xmlDoc.async = false
    xmlDoc.load(xmlFile)

    xmlDoc.load("sitenavigation.xml")

    If xmlDoc.parseError.errorCode <> 0 Then
          Response.Write("There was an error loading the xml file!<br>")
    Else
        Response.Write("xml file loaded.<br>")
        set doc = xmlDoc.documentElement
        traverse(doc)
    End If
end sub

sub traverse(tree)
    if tree.hasChildNodes then
        Response.Write("<ul><li>")
        Response.Write("<b>" & tree.tagName & " : </b>")
        nodes = tree.childNodes.length
        For i = 0 To tree.childNodes.length - 1
            traverse(tree.childNodes(i))
        Next
        Response.Write("</li></ul>")
    else
        Response.Write(tree.text)
    end if
   
end sub

call loadXML()

%>

Thank you,
-Greg
ASKER CERTIFIED SOLUTION
jkmyoung

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 12 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 12 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros