javascript - 从 xml 文件中获取子项的子项

标签 javascript arrays xml

所以我尝试从 xml 文件中提取数据,如下所示:

<example>
    <phrase>phrase1</phrase>
    <word>
      <definition>definition1</definition>
    </word>
    <word>
      <definition>definition2</definition>
    </word>
</example>

我希望能够将定义1和定义2作为数组的单独成员,现在我将它们像这样合并:definition1definition2

这是我的代码:

var $example = $xml.find("example");
                $example.each(function(){                   
        list.push($(this).children("word").children("definition").text());
                });

有人有想法吗? 谢谢

最佳答案

如果要使用JQuery,则需要更改

$example.each(function(){                   
        list.push($(this).children("word").children("definition").text());
                });

$example.children("word").children("definition").each(
  function() {                   
   list.push($(this).text());
  });

关于javascript - 从 xml 文件中获取子项的子项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38476361/

相关文章:

javascript - 无法按名称引用动态创建的元素

javascript - 更好地理解 AJAX

ruby-on-rails - 将 ruby​​ 数组的元素划分为精确数量的(几乎)大小相等的子数组

xml - 我如何拆分 <xsl :foreach> into multiple parts?

c# - XMLTextReader 使用什么解析方法?

javascript - Typescript - 移相器碰撞重叠功能不起作用

javascript - Cordova、Android 和无缝/无缝的声音循环

C++ 检查 float

arrays - 为什么 numpy 乘法不会将 (5,5) 和 (5,23) 数组相乘

mysql - 在 Ubuntu 12.04 上为 MySQL 配置 hibernate.cfg.xml