1

XML構造にjQuery Ajaxを使用して、XMLファイルから複数のアイテムを解析しようとしています:

<titles>
    <titleitem>
        <authors>
            <author>1</author>
            <author>2</author>
            <author>3</author>
        </authors>
    </titleitem>
</titles>

ここで、それぞれ<titleitem>に異なる数の著者がいます (または、上記の例には示されていませんが、トピックの数も異なります)。

私の正確なjQueryとXMLは以下の通りです。私の現在のコードで起こっていることは、その特定の<author>のみがリストされているのではなく、すべての が各アイテムに対してリストされていることです。これで私を助けていただければ、本当に感謝しています。<author><titleitem>

上記に加えて、それぞれ<author>に特定の URL と、それぞれに特定の URL が必要<topic>です。以下の XML にこれらの URL はまだありません。<author>と のそれぞれに特定の URL を追加する方法を教えていただけます<topic>か?

どうもありがとうございました!

XML:

<titles>

  <titleitem id="0">
    <title>This is one title</title>
    <subtitle>This is an example of a subtitle</subtitle>
    <authors>
      <author>Steve Johnson</author>
      <author>Michael Smith</author>
    </authors>
    <topics>
      <topic>Technology</topic>
    </topics>
    <imagelg type="html"><![CDATA[http://www.google.com/serverfiles/productimages/sf113072b.jpg]]></imagelg>
    <url type="html"><![CDATA[http://www.google.com/ProductDetail.aspx?ProductId=123456]]></url>
    <desc>
      <brief type="html"><![CDATA[This is a brief description]]></brief>
      <long type="html"><![CDATA[text here]]></long>
    </desc>
  </titleitem>

  <titleitem id="1">
    <title>This is another title</title>
    <subtitle>This is an example of a subtitle</subtitle>
    <authors>
      <author>John Williams</author>
    </authors>
    <topics>
      <topic>Management</topic>
      <topic>Info</topic>
      <topic>Systems</topic>
    </topics>
    <imagelg type="html"><![CDATA[http://www.google.com/serverfiles/productimages/sf113075b.jpg]]></imagelg>
    <url type="html"><![CDATA[http://www.google.com/ProductDetail.aspx?ProductId=123456]]></url>
    <desc>
      <brief type="html"><![CDATA[This is a brief description]]></brief>
      <long type="html"><![CDATA[text here]]></long>
    </desc>
  </titleitem>
<titles>

jQuery:

$.ajax({
  type: "GET", 
  dataType: "xml", 
  cache: false, 
  async: false, 
  url: xmlTitlesContent, 
  success: parTitlesCon, 
  error: parseError
});

function parTitlesCon(xml) {
  $(xml).find('titleitem').each(function(){
    var id         = $(this).attr('id');
    var title       = $(this).find('title').text();
    var subtitle     = $(this).find('subtitle').text();
    var imagelg     = $(this).find('imagelg').text();
    var url       = $(this).find('url').text();

    $('<div class="titleitems" id="link_'+id+'"></div>')
      .html('<div class="itemcontainer"><div class="itemsmimage"><a href="#"><img src="'+imagelg+'" alt="'+title+'" /></a></div><div class="itemcontent"><div class="itemtitle"><a href="#">'+title+'</a></div><div class="itemsubtitle">'+subtitle+'</div><div class="itemauthor"><span class="authorbytxt">By</span></div><div class="itemtopics"><div class="itemtopicstitle">Topics</div><div class="itemtopiclist"></div><div class="clear"></div></div><div class="clear"></div></div><div class="clear"></div></div>')
      .appendTo('#contentloaded');
    $(this).find('authors').each(function(){
      var author     = $(this).find('author').text();
      $('<a href="#"></a>').html(author).appendTo('div.itemauthor');
    });
    $(this).find('topics').each(function(){
      var topic     = $(this).find('topic').text();
      $('<span class="topic"></div>').html(topic).appendTo('div.itemtopiclist');
    });

  });
}

function parseError() {

  // Error Message
  var parseErrorMessage = 'There was a problem loading the content. Please try again later.';

  // Append Notice in Content Body
  $('#contentloaded').append('Not content available');

  // Append Popup to Body
  $('body').append('<div class="ariasXMLLoadError"><div class="ariasXMLLoadError_inside"><div class="ariasXMLLoadError_info">'+ parseErrorMessage +'<div class="clear"></div></div><div class="clear"></div></div><div class="clear"></div></div>');

  // FadeIn/FadeOut Popup
  $('div.ariasXMLLoadError').fadeIn(200).delay(3000).fadeOut('slow', function(){
    $(this).remove();
  });
}
4

2 に答える 2

2

試す

$(this).find('author').each(function(){
    var author = $(this).text();
    $('<a href="#"></a>').html(author).appendTo('#link_'+id+' div.itemauthor');
});

著者タグに関係なく著者タグを繰り返したいためです。また、現在のリンク div id を使用して、その div id のみに追加します。


$(this).find('author').each(function(){
    var name = $(this).find('name').text();
    var link = $(this).find('link').text();
    $('<a href="'+link+'"></a>').html(name).appendTo('#link_'+id+' div.itemauthor');
});
于 2013-08-05T14:15:22.000 に答える