我正在尝试使用 jQuery Ajax 为 xml 结构解析 XML 文件中的多个项目:
<titles>
<titleitem>
<authors>
<author>1</author>
<author>2</author>
<author>3</author>
</authors>
</titleitem>
</titles>
其中每个<titleitem>
作者都有不同数量的作者(或者,上面的示例中未显示,还有不同数量的主题)。
我的确切 jQuery 和 XML 如下。我当前的代码发生的情况是,<author>
每个项目都列出了所有 's,而不是仅列出<author>
了该特定项目的 's <titleitem>
。如果您能帮我解决这个问题,我将不胜感激。
除了上述之外,我还需要为 each 设置<author>
一个特定的 url,为 each 设置一个特定的 url <topic>
。我在下面的 XML 中还没有这些 url。你能告诉我如何为每个<author>
and添加特定的 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();
});
}