View Single Post
  #2 (permalink)  
Old August 29th, 2005, 03:39 AM
joefawcett's Avatar
joefawcett joefawcett is offline
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

On of your problems arises from the fact that nodes includes text nodes as well as elements.

Although learning XPath is a big step it makes extracting information so much easier and for simple documents it resembles a file path.
The path to the <participant>s would be:
Code:
/event/participant
so
Code:
function parseXML(theXML)
{
  var sXPath = "/event/participant";
  var colParticipant = theXML.selectNodes(sXPath);
  alert("Participant count: " + colParticipant.length);
  for (var i = 0; i < colParticipant.length; i++)
  {
    var oParticipant = colParticipant[i];
    var oFirstname = oParticipant.selectNodes("firstname")[0];
    var oLastname = oParticipant.selectNodes("lastname")[0];
    alert(oFirstname.childNodes[0].nodeValue + " " + oLastname.childNodes[0].nodeValue);
  }  
}
Thre are hundreds of tutorials on XPath, try W3Schools for starters.

--

Joe (Microsoft MVP - XML)