views:

133

answers:

2

I'm spinning my wheels on this. How do I get the values from the following nested elements from the XML below (I've also put my code below)? I am after the "descShort" value and then the capital "Last" and capital "change" :

<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');                                                                   
                });
            }
        });
A: 
success: function(xml) {
  $(xml).find('index indexDesc descShort').each(function(){                    

    $('<span class=\"tickerItem\"></span>').html($(this).text()).appendTo('#marq');                                                                   
  });
}

Second requested example (this is the most readable, some optimisation possible)

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                                                                  
  });
}
James Westgate
Hi, thanks. I was also hoping to get the value for:'index indexQuote capital last'. Can I do this somehow so that it is all done in one iteration along with the descShort value your provided me with?
Dkong
Ill add that example
James Westgate
thanks James. For some reason (using the exact above code and XML, and even after double checking it), the 'indexDesc descShort' text is coming back empty ( definitely no typos). Bizzaro
Dkong
I dont see a closing </indices> tag on the xml you pasted in
James Westgate
Hi James, well spoted yes that should be there, but even when it is, this line is still not reading anything:var desc = $('indexDesc descShort', this).text();very odd
Dkong
Theres something else going on since both samples of code are producing the same error.
James Westgate
+1  A: 

You have some flaws in your function. This should do it:

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');
    });
}

Two comments on your code:

var code = self.find('indexDesc');

$(code).find('indexDesc').each(function(){
       alert(self.find('descShort').text());
}); 

Here you assign the element indexDesc to the variable code and later you try to find the element indexDesc inside indexDesc (which does not exist).

$('<span class=\"tickerItem\"></span>').html(values[0].text())

I really wonder where values comes from, it is never declared. And you don't need to escape double quotes in single quotes.


I really recommand to read the documentation and a tutorial to get the basics of jQuery.

Felix Kling
Hi Felix. Thanks for your insight. Much appreciated.
Dkong
Felix, I've just tested it and I notice that the part that retreives the 'indexDesc descShort' is coming back empty for some reason. Very odd.
Dkong
@Dkong: Mmh I don't know. Based on the XML document you show, it should work.
Felix Kling