开发者

Manipulating Data from an XML feed using jQuery

I'm fetching XML data and including it in a HTML file using jQuery, and I'm a little stuck on trying to manipulate the format.

Allow me to provide some more data on what I'm doing:

I've got some jQuery to fetch the external XML:

<script type="text/javascript">
<!--
jQuery.fn.xml=function(a){
    var b="";
    if(this.length)(typeof a!="undefined"&&a?this:jQuery(this[0]).contents()).each(function(){
        b+=window.ActiveXObject?this.xml:(new XMLSerializer).serializeToString(this)});
        return b
    }
    $(document).ready(function(){
        $('div.cnetxml').each(function(index) {
            var var_url = $(this).attr('data');
            var var_digcontent_name = $(this).attr('id');
            var obj_divcnetxml = $(this);
            $.getJSON("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D%22"+encodeURIComponent(var_url)+"%22&format=xml'&callback=?",function(data){
                if(data.results[0]){
                    var data = data.results[0];
                    obj_divcnetxml.html($( $.parseXML( data ) ).find( "ul" ).xml());
                    };
                    obj_divcnetxml.removeClass('hide');
                }
            }
        );                                
    });
});
-->
</script>
<div class="cnetxml hide" data="http://cdn.cnetcontent.com/30/7f/307f280c-15f2-4e3e-9391-4e09c9ecc450.xml" id="cnet_DigitalContentProductDescription"> </div>

Check out the XML file to see the format.

The resulting format looks similar to this, without the unorder list wrapping tag:


<li>Item 01开发者_运维技巧 Header</li>
<li>Item 01 Description</li>
<li>Item 02 Header</li>
<li>Item 02 Description</li>
<li>Item 03 Header</li>
<li>Item 03 Description</li>
…and so on

The list items appear in header/description pairs, and I'd like to combine these pairs in the following format:


<li><strong>Item 01 Header</strong><br />
Item 01 Description</li>
<li><strong>Item 02 Header</strong><br />
Item 02 Description</li>
<li><strong>Item 03 Header</strong><br />
Item 03 Description</li>

Also, I need to wrap the list in the appropriate <ul> tags.

Any clever jQuery people out there who could spare a minute to help. I'll put my hands up, I'm not too great with this stuff.


This is the full code that works.

<script type="text/javascript">
<!--
jQuery.fn.xml=function(a){
    var b="";
    if(this.length)(typeof a!="undefined"&&a?this:jQuery(this[0]).contents()).each(function(){
        b+=window.ActiveXObject?this.xml:(new XMLSerializer).serializeToString(this)});
        return b
    }
    $(document).ready(function(){
        $('div.cnetxml').each(function(index) {
            var var_url = $(this).attr('data');
            var var_digcontent_name = $(this).attr('id');
            var obj_divcnetxml = $(this);
            $.getJSON("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D%22"
                   +encodeURIComponent(var_url)+"%22&format=xml'&callback=?",function(data){
                if(data.results[0]){
                    var data = data.results[0];
                    obj_divcnetxml.html($( $.parseXML( data ) ).find( "ul" ).xml());
                };
                obj_divcnetxml.removeClass('hide');
                $("div#cnet_DigitalContentProductDescription li").each(function() {
                       $(this).html("<strong>" + $(this).html() + "</strong><br />" + $(this).next("li").html());
                       $(this).next("li").remove();
                })
            })
        })
    });
-->
</script>
<div class="cnetxml hide" data="http://cdn.cnetcontent.com/30/7f/307f280c-15f2-4e3e-9391-4e09c9ecc450.xml" id="cnet_DigitalContentProductDescription"> </div>


$(function(){
        $('ul li:nth-child(odd)').contents().filter(function() { return this.nodeType == 3; }).after('<br/>');
        $('ul li:nth-child(odd)').contents().filter(function() {
          return this.nodeType == 3;
        }).wrap('<strong></strong>')
$('ul li:nth-child(odd)').each(function(){
   $(this).append($(this).next().text())
})
$('ul li:nth-child(even)').remove()
})
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜