I'm trying to parse multiple items from an XML file using jQuery Ajax for the xml structure:
<titles>
<titleitem>
<authors>
<author>1</author>
<author>2</author>
<author>3</author>
</authors>
</titleitem>
</titles>
where each <titleitem> has a different number of authors (or, not shown in the example above, also a varying number of topics).
My exact jQuery and XML are below. What is happening with my current code is that all <author>'s are being listed for each item, instead of only the <author>'s for that specific <titleitem> being listed. If you can help me with this, I would really appreciate it.
In addition to the above, I also need to have a specific url for each <author> and a specific url for each <topic>. I don't have these urls in the below XML yet. Could you show me how to add that specific URL for each <author> and <topic>?
Thank you very much!
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();
});
}
This is an example of a subtitle Steve Johnson Michael Smith Technology This is an example of a subtitle John Williams Management Info Systems