?
Solved

Looping Through XML Data In jQuery

Posted on 2008-11-03
4
Medium Priority
?
910 Views
Last Modified: 2013-11-11
I am currently working on a project for work which uses jQuery and XML to fetch and display data obviously from an XML file. I've got about 90% of the project done, but I've hit a roadblock and don't know how to get around it.

Although I am relatively new to jQuery (not Javascript), I've picked up a lot of things myself.

In the XML file there is the following data:

      <item>
            <title>Blog Title</title>
            <imageurl>image_background.png</imageurl>
            <rssurl>rss.xml</rssurl>
            <blogurl>/blog/</blogurl>
            <contributors>
                  <contributor imageurl="img/rebekah.png" profileurl="">Rebekah</contributor>            
                  <contributor imageurl="img/sarah.png" profileurl="">Sarah</contributor>
                  <contributor imageurl="img/josh.png" profileurl="">Josh</contributor>      
                  <contributor imageurl="img/cassie.png" profileurl="">Cassie</contributor>      
            </contributors>
      </item>

In the XML file each <item></item> is a new blog. At present there are six test blogs in my XML file and I am trying to retrieve the contributor images and then append them to the document via the .append() function. The problem is addressing the specific contributor images in each <item></item>. I have no problem retrieving the first contributor images, but I am having issues getting the other contributor images for other <item></item>'s.

Each item is contained within a case statement. As you can see from the code supplied that it's starting at zero and zero is obviously the first XML element.

Any idea's on how to effectively do this so that contributor images are properly displayed as per the item they are from?


var contrib_count = $('item:eq(1)>contributors>contributor',xml).length;
$(".contributor-container").remove();					$(".contributor_img").remove();		
$(".contributor_url").remove();
						
var i = 0;
while(i < contrib_count)
{							  _contributor_img = $('contributor:eq('+i+')',xml).attr("imageurl");
_contributor = $('contributor:eq('+i+')',xml).text();
							$("#contributors").append('<span class="contributor-container"> <a href="javascript:void(0);"><img src="'+_contributor_img+'" class="contributor_img" /></a> <a href="javascript:void(0);" class="contributor_url">'+_contributor+'</a> </span>');
i++;
						}

Open in new window

0
Comment
Question by:dwaynecharrington
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 16

Accepted Solution

by:
sh0e earned 1500 total points
ID: 22868201

$('item', xml).each(function(){
    $('contributors>contributor',this).each(function(){
        _contributor_img = $(this).attr("imageurl");
        _contributor = $(this).text();
        $("#contributors").append('<span class="contributor-container"> <a href="javascript:void(0);"><img src="' + _contributor_img + '" class="contributor_img" /></a> <a href="javascript:void(0);" class="contributor_url">' + _contributor + '</a> </span>');
    });
});

Open in new window

0
 
LVL 6

Author Comment

by:dwaynecharrington
ID: 22870944
Hey sh0e,

Thanks for your prompt resonse again. I tried the code you gave me and it seems to display all contributors and their images.

Seeming as these functions are called from case statements. How do I make it only display the contributors from each item in the XML file? Say for example: Case '1' obviously calls item number 0 in the XML file, how do I make it only pull out the first contributor nodes for the first item?

Thanks!
0
 
LVL 6

Author Comment

by:dwaynecharrington
ID: 22872722
Sh0e's solution works perfectly. I just realised that for individual nodes you must change the number you want to target:

This code will target all contributor nodes for the first item. You simple replace the eq(0) with the number of the element you want to target.

$('item:eq(0)', xml).each(function(){
    $('contributors>contributor',this).each(function(){
        _contributor_img = $(this).attr("imageurl");
        _contributor = $(this).text();
        $("#contributors").append('<span class="contributor-container"> <a href="javascript:void(0);"><img src="' + _contributor_img + '" class="contributor_img" /></a> <a href="javascript:void(0);" class="contributor_url">' + _contributor + '</a> </span>');
    });
});

Open in new window

0
 
LVL 6

Author Closing Comment

by:dwaynecharrington
ID: 31512625
Thank you. After a little reading and thought, I worked out how to customise the code to my liking.
0

Featured Post

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question