954,568 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Parse an XML document with AJAX

Hi,

I have a Javascript function that creates an Ajax request, returning an XML document into transport.responseXML . I want to process this document but without hardcoding each tag name i.e. xmlOptions[0].getElementsByTagName('salary')[0].firstChild.data . I would like to iterate over the XML document and find each tag and its value. The XML document I want to process is structured as such:

<job>
   <contactTelephone>028 ---------</contactTelephone>
   <contactName>Karen -------</contactName>
   <contactFax>028 -----------</contactFax>
   <contactEmail>karen@---------</contactEmail>
   <contactWebsite>www.---------</contactWebsite>
   </job>


Thanks,

Sean

Sebarry
Newbie Poster
1 post since Jul 2007
Reputation Points: 10
Solved Threads: 0
 

Just loop through the array like you would any other array. See that zero? It can be a variable, too.

for (var i = 0; i <  xmlOptions[0].getElementsByTagName('salary').length; i++)
{
     document.write(xmlOptions[0].getElementsByTagName('salary')[i]);
}
stymiee
He's No Good To Me Dead
Moderator
3,360 posts since May 2006
Reputation Points: 161
Solved Threads: 38
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You