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
Error Type:
Microsoft VBScript runtime (0x800A01B6)
Object doesn't support this property or method: 'tree.hasChildNodes'
/xml3.asp, line 23
<%
Dim xmlDoc
Set xmlDoc = Server.CreateObject("Micro
sub loadXML()
xmlDoc.async = false
xmlDoc.load("sitenavigatio
If xmlDoc.parseError.errorCod
Response.Write("There was an error loading the xml file!<br>")
Else
Response.Write("xml file loaded.<br>")
'set doc = xmlDoc.documentElement
Set xmlNode = xmlDoc.selectNodes("//sect
'traverse(doc)
traverse(xmlNode)
TotalNodes = cint(xmlNode.length)
Response.Write "TotalNodes = " & TotalNodes & "<br><br>"
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()
%>