jQuery usuario obtener elementos anidados de XML
-
28-09-2019 - |
Pregunta
Estoy girando mis ruedas en esto. ¿Cómo consigo los valores a partir de los siguientes elementos anidados desde el XML siguiente (también he puesto mi código de abajo)? Soy después de que el valor de "descShort" y luego la capital "Última" y capital "cambio":
<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');
});
}
});
Solución
tiene algunos defectos en su función. Esto debe hacerlo:
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');
});
}
Dos comentarios en su código:
var code = self.find('indexDesc');
$(code).find('indexDesc').each(function(){
alert(self.find('descShort').text());
});
A continuación, se asigna el indexDesc
elemento al code
variable y después intenta encontrar el indexDesc
elemento dentro de indexDesc
(que no existe).
$('<span class=\"tickerItem\"></span>').html(values[0].text())
Me pregunto dónde proviene de values
, nunca se declara. Y no es necesario para escapar comillas dobles comillas simples.
Realmente recommand de leer la documentación y una tutorial para obtener las bases de jQuery.
Otros consejos
success: function(xml) {
$(xml).find('index indexDesc descShort').each(function(){
$('<span class=\"tickerItem\"></span>').html($(this).text()).appendTo('#marq');
});
}
Segundo ejemplo solicitó (este es el más fácil de leer, una optimización posible)
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
});
}