views:

29

answers:

2

So, I have a list of nodes in a dynamic XML that is cached on a server. Using Ajax, I loop through the particular nodes to return a string each time:

XML:

<?xml version="1.0"?>
<Products>
    <Product>
        <ItemName>String</ItemName>
    </Product>
    <Product>
        <ItemName>String</ItemName>
    </Product>
    <Product>
        <ItemName>String</ItemName>
    </Product>
<Products>

jQuery:

$.ajax({
type: "GET",
url: '/services/Scraper.aspx',
success: function(data) {
    $(data).find('Product').each(function() {

        var itemSrc = $(this).find('ItemName').text();

    });
}
});

How do I go about injecting each one of those strings in order into my H2 tag below (assuming there can be more than three XML nodes and/or HTML H2 tags?

<div class="itemLoc">
    <h2></h2>
</div>
<div class="itemLoc">
    <h2></h2>
</div>
<div class="itemLoc">
    <h2></h2>
</div>

Any help would be great! Thanks!

A: 

how about using XSLT instead of javascript for processing? http://www.w3schools.com/xsl/default.asp

this is exactly what it's intended for (transforming XML to some other format)

i know it's not what you asked but in case you weren't aware :-)

mr.nicksta
Thanks for the info. And yes, I was aware of this, however the XML is populated dynamically, so I don't actually have access to it.
Jim
if it follows a set format or schema though you don't need access, just need to build the XSLT off the back of this
mr.nicksta
+1  A: 

Thanks guys, but I figured it out:

I added an index to the loop and then set that index to the location of the h2:

$(data).find('Product').each(function(i) {     
    var itemDescSrc = $(this).find('ItemName').text();
    var itemDescLoc = $('div.itemLoc h2');
    itemDescLoc.eq(i).text(itemDescSrc);
});
Jim
ALthough the above code worked. And does work for all browsers, IE for whatever reason, does not want to run the loop inside of the ajax success. Every other browser runs the loop just fine, but IE seems to ignore the .each() function... Any ideas anyone?
Jim