Link to home
Start Free TrialLog in
Avatar of tamilgirl77
tamilgirl77

asked on

Select value of attribute in xml through ASP

Can you someone help with how to get the value of date to be displayed right before the description in the code below,

thank you,

xml:
<%
strDate = "4/18/2006"
intMaxEvents = 3


Set oXMLHTTP = Server.CreateObject("Microsoft.XMLDOM")
oXMLHTTP.async = False
<?xml version="1.0" encoding="UTF-8"?>
<calendar>
     <events>
          <event date="4/17/2006">
               <description>Party</description>
          </event>
          <event date="4/20/2006">
               <description>Work</description>
          </event>
          <event date="4/17/2006">
               <description>Run Marathon</description>
          </event>
          <event date="4/17/2006">
               <description>Recover</description>
          </event>
          <event date="4/20/2006">
               <description>Another Item on another day</description>
          </event>
     </events>
</calendar>

ASP:

If oXMLHTTP.Load(Server.MapPath("calendar.xml")) Then
     Set oEventNodes = oXMLHTTP.documentElement.selectNodes("//event[@date>='" & strDate & "']")
     Response.Write "<ul>"    
     For i = 0 To (oEventNodes.Length - 1)
          Response.Write "<li>" & oEventNodes.Item(i).SelectSingleNode("description").Text & "</li>"
          If i = (intMaxEvents - 1) Then Exit For
     Next    
     Response.Write "</ul>"
Else
     Response.Write "XML file did not load"
End If
Set oXMLHTTP = Nothing
%>
ASKER CERTIFIED SOLUTION
Avatar of peterxlane
peterxlane

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of deighc
deighc

- oEventNodes is a collection of <event> node objects.
- A node object has an attributes collection, and this collection has a getNamedItem method which returns an attribute object
- The attribute object has a value property

So, to access the 'date' attribute of the <event> node inside your For...Next loop try something like this

oEventNodes.item(i).atttributes.getNamedItem("date").value
@deighc - Is there an advantage to doing it the way you suggested compared to the way I did it?  I work a fair amount with XSL, so it seemed intuitive for me to just provide an XPath allow with the SelectSingleNode method, but I am curious if your way may have some advantages that I am not aware of...
> Is there an advantage to doing it the way you suggested compared to the way I did it?

No.

It was my understanding that attributes could only be accessed programatically through the attributes collection. I didn't realise that they were also accessible by using the SelectSingleNode() method. But this makes sense since an XML parser builds a 'node tree' from all element and attribute nodes.

So:
- I've learned something
- @tamilgirl77, please don't accept my answer over @peterxlane's as they're functionally identical
Avatar of tamilgirl77

ASKER

Thank you both for helping me out,