使用Jquery从XML查找特定节点的子元素 [英] Find Particular Node's Child Element From XML Using Jquery

查看:214
本文介绍了使用Jquery从XML查找特定节点的子元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个xml文件,例如

i have a xml file like

<?xml version="1.0" encoding="ISO-8859-1"?>
<childrens>
<child id="1" value="Root Catalog" parent_id="0">
  <child id="2" value="Apparel" parent_id="1">
  <child id="4" value="Shirts" parent_id="2"/>
<child id="5" value="Pants" parent_id="2"/>
</child>
<child id="3" value="Accessories" parent_id="1">
  <child id="6" value="Handbags" parent_id="3"/>
  <child id="7" value="Jewelry" parent_id="3"/>
</child>
 .
 .
 .
<child id='1110'>
   <child id='1111' value="test" parent_is="1110">
       <child id="1005" value="test1" parent_is="1111"/>
       <child id="1006" value="test12" parent_is="1111"/>
       <child id="1007" value="test123" parent_is="1111"/>
    <child>
</child>
<child >
</childrens>

我正在使用jquery来查找特定的子节点

i am using jquery to find a particular nodes children

我已编写此代码来查找id=1111value=test

i have written this code to find the child of the node where id=1111 and value=test

$(document).ready(function(){
    $.ajax({
        type: "GET",
        url: "test.xml",
        dataType: "xml",
        success: function(xml) {
            $(xml).find('child[value="test"]').each(function(){
                var i = $(this).attr('value');
                alert(i);

            });
        }
    });
});

它只给我一个名字,它就是我自己

it gives me only one name which is it self

在输出中我需要三个名称是它的子元素

while i need in output three name which is its child element

test1tets12test123

推荐答案

我想您要使用 .children() .

I suppose you want to use .children().

$(xml).find('child[value="test"]').children().each(function(){
    ...
});

这篇关于使用Jquery从XML查找特定节点的子元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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