2011-08-03 27 views
0

XMLデータを取得してjQueryを使用してHTMLファイルに組み込んでいますが、フォーマットを操作しようとしています。jQueryを使用してXMLフィードからデータを操作する

私は私がやっているものにいくつかのより多くのデータを提供することを許可する:

は、私はいくつかのjQueryを外部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形式を参照してください。結果の形式は次のようになり

、unorderリストラッピングタグなし:


<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 

リスト項目は、ヘッダ/記述のペアで表示され、私は次の形式でこれらのペアを結合したいと思います:


<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> 
 

また、適切な<ul>タグでリストを囲む必要があります。

賢いjQueryの人々は誰でも手伝ってくれます。私は私の手を上げます、私はこのものであまりにも偉大ではないです。

+0

あなたはXMLを取得していることを確認していますか?それはHTMLのように見えるので... XHTMLの場合を除き、整形式なのでOKです。 –

+0

こんにちはVivin、私は自分のしていることをよりよく説明するために私のオリジナルのステートメントを編集しました。これがあなたに役立つことを願っています。 –

答えて

3

これは動作する完全なコードです。

<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> 
+0

あなたのバージョンは私のものよりもずっと簡単でクリーンです...それをキャッチしてください:D Thumb up :) –

0
$(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() 
}) 
関連する問題