使用jQuery从XML获取嵌套项目 [英] Get nested item from XML with jQuery

查看:85
本文介绍了使用jQuery从XML获取嵌套项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在网上查看了一些示例,但是我仍在为此而苦苦挣扎. 我想在" indexDesc "标签中获取" descShort "标签的值,然后在"最后一个"中显示该值"标签?我见过人们使用箭头>,但我仍然迷路.

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>

推荐答案

>"是选择器;您可以在此处查看所有可用的选项:选择器. "div> span"将找到所有以div为父对象的span.这与"div span"不同,"div span"将找到所有属于div的后代.

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现在是一个对象数组,每个对象具有descShort,captialLast和GrossLast属性.

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

不幸的是,我无法测试我的代码.

Unfortunately I can't test my code.

这篇关于使用jQuery从XML获取嵌套项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆