使用AJAX循环遍历动态XML节点并使用jQuery注入一组h2元素

时间:2022-12-04 16:05:39

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中的节点列表。使用Ajax,我遍历特定节点,每次都返回一个字符串:

XML:

XML:

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

jQuery:

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?

我如何将这些字符串中的每一个按顺序注入下面的H2标签(假设可以有三个以上的XML节点和/或HTML H2标签?

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

Any help would be great! Thanks!

任何帮助都会很棒!谢谢!

2 个解决方案

#1


1  

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:

我在循环中添加了一个索引,然后将该索引设置为h2的位置:

$(data).find('Product').each(function(i) {     
    var itemDescSrc = $(this).find('ItemName').text();
    var itemDescLoc = $('div.itemLoc h2');
    itemDescLoc.eq(i).text(itemDescSrc);
});

#2


0  

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

如何使用XSLT代替javascript进行处理? http://www.w3schools.com/xsl/default.asp

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

这正是它的目的(将XML转换为其他格式)

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

我知道这不是你问的,但万一你不知道:-)

#1


1  

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:

我在循环中添加了一个索引,然后将该索引设置为h2的位置:

$(data).find('Product').each(function(i) {     
    var itemDescSrc = $(this).find('ItemName').text();
    var itemDescLoc = $('div.itemLoc h2');
    itemDescLoc.eq(i).text(itemDescSrc);
});

#2


0  

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

如何使用XSLT代替javascript进行处理? http://www.w3schools.com/xsl/default.asp

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

这正是它的目的(将XML转换为其他格式)

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

我知道这不是你问的,但万一你不知道:-)