0

I've looked at some examples on the web but I am still struggling with this. I would like to get the value for "descShort" tag within the "indexDesc" tag and then after that display the value from the "last" tag? I've seen people using the arrow > but I'm still lost.

<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>
</indices>
2
  • You have to describe it better. There are more than one last tag and how do you want the output to be like? Can you provide an example? Commented Apr 19, 2010 at 17:46
  • sorry, I would like just the "last" value from "gross" parent tag along with the previous shortDesc Commented Apr 19, 2010 at 18:58

1 Answer 1

1

The ">" is a selector; you can see all f the available ones here: selectors. "div > span" will find all spans that have div's as parents. This is different to "div span", which will find all spans which are descendents of divs.

var values = [];

$(yourXml).find('index').each(function () {
  var self = $(this);

  values.push({
    descShort: self.find('descShort:first').text(),
    capitalLast: self.children('capital').children('last').text(),
    grossLast: self.children('gross').children('last').text()
  });
});

values is now an array of objects, each with descShort, captialLast and grossLast properties.

Unfortunately I can't test my code.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks for your answer. the values are coming up empty for some reason. it keeps saying 'undefined' when i trt print the value. ie alert(self.find('descShort:first').val());
Try .text() or .html() instead. Not sure why I used .val() in the first place tbh :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.