在Jquery中解析XML [英] XML parsing in Jquery

查看:149
本文介绍了在Jquery中解析XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个XML文件,其中有四个< resutGroups> 标签:

I have an XML file which has four <resutGroups> tag:

<resultGroups>
  <subGroups>
    <name> </name>
  </subGroups>
  <name> </name>
</resultGroups>

每个< resultGroup> code>< subGroups> ,每个< subGroups> < name> 标签。

each <resultGroup> has several <subGroups> and each <subGroups> has <name> tag.

我只想选择< resultGroups> 的名称标签

$(xml).find("resultGroups").each(function() {
  alert( $(this).find("name").text() ); 
}

上面的代码,它返回< resultgroups> < subGroups> 中的所有名称。

When I use the above code it returns all the names inside the <resultgroups> and <subGroups>.

如何只选择< resultGroups> < name> c $ c>标签?

How can I select only one <name> which is in the <resultGroups> tag?

推荐答案

您有以下几种选择:

var xml = $(xml);
$('resultGroups > name', xml).each(function() {
    alert($(this).text());
});

=http://docs.jquery.com/Selectors/child#parentchild =noreferrer>直接后裔选择器。您也可以使用 儿童 同样的事情:

This uses the direct descendant selector. You could also use children, which does the same thing:

$('resultGroups', xml).children('name').each(function() {
    alert($(this).text());
});

这篇关于在Jquery中解析XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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