Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
Password Reminder
Register
| FAQ | Members List | Calendar | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #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
Reply With Quote
  #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)
Reply With Quote
  #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.

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 03:25 AM.


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