我在转动轮子。如何从下面的XML中获取以下嵌套元素中的值(我还将代码放在下面)?我追随“descShort”值,然后是资本“Last”和资本“改变”:
<indices>
<index>
<code>DJI</code>
<exchange>NYSE</exchange>
<liveness>DELAYED</liveness>
<indexDesc>
<desc>Dow Jones Industrials</desc>
<descAbbrev>DOW JONES</descAbbrev>
<descShort>DOW JONES</descShort>
<firstActive></firstActive>
<lastActive></lastActive>
</indexDesc>
<indexQuote>
<capital>
<first>11144.57</first>
<high>11153.79</high>
<low>10973.92</low>
<last>11018.66</last>
<change>-125.9</change>
<pctChange>-1.1%</pctChange>
</capital>
<gross>
<first>11144.57</first>
<high>11153.79</high>
<low>10973.92</low>
<last>11018.66</last>
<change>-125.9</change>
<pctChange>-1.1%</pctChange>
</gross>
<totalEvents>4</totalEvents>
<lastChanged>16-Apr-2010 16:03:00</lastChanged>
</indexQuote>
</index>
<index>
<code>XAO</code>
<exchange>ASX</exchange>
<liveness>DELAYED</liveness>
<indexDesc>
<desc>ASX All Ordinaries</desc>
<descAbbrev>All Ordinaries</descAbbrev>
<descShort>ALL ORDS</descShort>
<firstActive>06-Mar-1970</firstActive>
<lastActive></lastActive>
</indexDesc>
<indexQuote>
<capital>
<first>5007.30</first>
<high>5007.30</high>
<low>4934.00</low>
<last>4939.40</last>
<change>-67.9</change>
<pctChange>-1.4%</pctChange>
</capital>
<gross>
<first>5007.30</first>
<high>5007.30</high>
<low>4934.00</low>
<last>4939.40</last>
<change>-67.9</change>
<pctChange>-1.4%</pctChange>
</gross>
<totalEvents>997</totalEvents>
<lastChanged>19-Apr-2010 17:02:54</lastChanged>
</indexQuote>
</index>
$.ajax({
type: "GET",
url: "stockindices.xml",
dataType: "xml",
success: function(xml) {
$(xml).find('index').each(function(){
var self = $(this);
var code = self.find('indexDesc');
$(code).find('indexDesc').each(function(){
alert(self.find('descShort').text());
});
$('<span class=\"tickerItem\"></span>').html(values[0].text()).appendTo('#marq');
});
}
});
答案 0 :(得分:2)
你的功能有一些缺陷。这应该这样做:
success: function(xml) {
$(xml).find('index').each(function(){
var value = $(this).find('indexDesc descShort').text();
value += ' ' + $(this).find('indexQuote capital last').text();
value += ' ' + $(this).find('indexQuote capital change').text();
$('<span class="tickerItem"></span>').text(value).appendTo('#marq');
});
}
对您的代码有两条评论:
var code = self.find('indexDesc');
$(code).find('indexDesc').each(function(){
alert(self.find('descShort').text());
});
您可以在此处将元素indexDesc
分配给变量code
,然后尝试在indexDesc
内找到元素indexDesc
(不存在)。
$('<span class=\"tickerItem\"></span>').html(values[0].text())
我真的很想知道values
来自哪里,它永远不会被宣布。而且你不需要用单引号转义双引号。
我真的建议您阅读documentation和tutorial以获取jQuery的基础知识。
答案 1 :(得分:0)
success: function(xml) {
$(xml).find('index indexDesc descShort').each(function(){
$('<span class=\"tickerItem\"></span>').html($(this).text()).appendTo('#marq');
});
}
第二个请求的示例(这是最可读的,可能的一些优化)
success: function(xml) {
$(xml).find('index').each(function(){
var desc = $('indexDesc descShort', this).text();
var last = $('indexQuote capital last', this).text();
var change = $('indexQuote capital change', this).text();
//Do whatever with the text values
});
}