Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old August 28th, 2005, 09:19 PM
Authorized User
 
Join Date: Dec 2004
Location: Chesterfield, MO, USA.
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default Parsing XML with Javascript

I am working on a web application using the xmlhttprequest object. The returned xml looks something like this

<event>
   <name>Event Name</name>
   <start>August 10, 2005</start>
   <end>August 11, 2005</end>
   <participant>
      <firstname>John</firstname>
      <lastname>Doe</lastname>
   </participant>
   <participant>
      <firstname>Jane</firstname>
      <lastname>Doe</lastname>
   </participant>
</event>

I wrote a function that parses the returned XML but can't figure out how to return the elements in between the participant tags. Here's what I have so far

function parseXML(theXML) {
   var participants = theXML.getElementsByTagName("participant");
}

When I do a count of childnodes for each participants object, I would expect to see 2 childnodes, but instead I get much more.

For example, using the code snippet alert(participants[0].childNodes.length), I would expect to see 2 (firstname and lastname). Is there another way to do this, or is there any websites that you can direct me to that will give me some in depth information on parsing XML documents.

Thanks
Robbert van Andel
  #2 (permalink)  
Old August 29th, 2005, 03:39 AM
joefawcett's Avatar
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)
  #3 (permalink)  
Old August 29th, 2005, 09:37 AM
Authorized User
 
Join Date: Dec 2004
Location: Chesterfield, MO, USA.
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you, this is exactly what I needed.



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problems parsing XML Feed in Javascript kwilliams Classic ASP Professional 0 December 18th, 2007 06:33 PM
Problems parsing XML Feed in Javascript kwilliams Javascript 1 November 7th, 2007 05:59 PM
parsing xml kri_hegde XML 5 July 24th, 2007 11:37 AM
XML Parsing SCADA_Monkey XML 2 March 11th, 2007 03:20 PM
XML Parsing tgopal .NET Web Services 1 June 15th, 2004 03:25 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.