【问题标题】:Get nested item from XML with jQuery使用 jQuery 从 XML 中获取嵌套项
【发布时间】:2010-04-19 17:37:09
【问题描述】:

我查看了网络上的一些示例,但我仍在为此苦苦挣扎。 我想在“indexDesc”标签中获取“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>
</indices>

【问题讨论】:

  • 你必须更好地描述它。有多个last 标签,您希望输出是什么样的?你能举个例子吗?
  • 抱歉,我只想要来自“gross”父标签的“last”值以及之前的 shortDesc

标签: jquery xml


【解决方案1】:

“>”是一个选择器;你可以在这里看到所有可用的:selectors。 “div > span”将查找所有以 div 为父对象的 span。这与“div span”不同,后者将查找所有作为 div 后代的 span。

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 属性。

很遗憾,我无法测试我的代码。

【讨论】:

  • 感谢您的回答。由于某种原因,这些值是空的。当我打印值时,它一直说“未定义”。即 alert(self.find('descShort:first').val());
  • 尝试 .text() 或 .html() 代替。不知道为什么我首先使用 .val() tbh :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-05-09
  • 2022-12-03
  • 2013-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多