我使用以下代码在我的 Phonegap Android 应用程序中使用 jQuery 解析 XML 文件:
$( function() {
var XMLURI = 'http://gdata.youtube.com/feeds/api/users/remcob00/uploads?max-results=50&alt=rss&lr=nl&orderby=published';
$.ajax({
url: XMLURI,
success: function(xml){
setTimeout(
function(){
feedItem = '';
$(xml).find( "item" ).each(
function(){
var item = $(this),
title = item.find('title').text(),
link = item.find('link').text(),
description = item.find('description').text(),
pubDate = item.find('pubDate').text();
var idname = "v";
idname = idname.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+idname+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec(link);
var videoid = results[1];
feedItem = feedItem + '<li data-corners="false" data-shadow="false" data-iconshadow="true" data-wrapperels="div" data-icon="arrow-r" data-iconpos="right" data-theme="a" class="ui-btn ui-btn-icon-right ui-li-has-arrow ui-li ui-li-has-thumb ui-btn-up-a"><div class="ui-btn-inner ui-li"><div class="ui-btn-text">';
feedItem = feedItem + '<a href="video.html?v=' + videoid + '&title=' + encodeURIComponent(item.find('title').text()) + '&description=' + encodeURIComponent(description) + '" rel="external" class="ui-link-inherit">';
feedItem = feedItem + '<img src="http://img.youtube.com/vi/' + videoid + '/0.jpg" class="ui-li-thumb">';
feedItem = feedItem + '<h3 class="ui-li-heading">' + title+ '</h3>';
feedItem = feedItem + '</a></div><span class="ui-icon ui-icon-arrow-r ui-icon-shadow"> </span></div></li>';
});
$('#list').append(feedItem);
}
, 5000);
},
error: function(){
// $('#blogheader').html('<li>Kan video's niet laden, mogelijk heb je geen internet verbinding</li>');
},
dataType: "xml"
});
});
此代码将项目的标题和描述打印两次而不是一次。