我想专门从xml返回一个数据。返回一个具体的数据
如果nameOfBusiness =餐厅,那么它返回所有评级(三家餐厅)与一个具有餐厅的物品名称。
我怎样才能让它只警觉一个餐厅,而不包括其他2餐厅(与蒸汽和起源)?
这里是我的xml:
<resultitem>
<itemname>Restaurant</itemname>
<rating>3</rating>
</resultitem>
<resultitem>
<itemname>Restaurant Origins</itemname>
<rating>4</rating>
</resultitem>
<resultitem>
<itemname>Restaurant Steams</itemname>
<rating>4.1</rating>
</resultitem>
这里是我的Ajax代码:
$.ajax({
type: 'GET',
url: 'someURL.xml',
dataType: 'xml',
success: function(xml) {
var found = $(xml).find('resultitem itemname:contains("' + nameOfBusiness + '")');
if (!found.length) {
alert("Rating not published yet");
return;
}
found.each(function() {
var rating = $(this).parent().find('rating').text();
alert("The rating for " + nameOfBusiness + " is " + rating);
})
}
});
,而不是'found.each'循环只是处理第一项'发现[0]' –