jQuery:XML-从特定节点读取子节点 [英] jQuery : XML - Reading child nodes from a specific node

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

问题描述

我有一个xml文件,我想在其中读取特定节点的子节点...下面是XML文件.

I have an xml file in which i want to read the child nodes of a specific node... Below is the XML file.

XML文件结构:

<?xml version="1.0" encoding="ISO-8859-1"?>

<categories>
<category type='Type A'>
    <genre>1</genre>
    <genre>2</genre>
</category>
<category type='Type B'>
     <genre>3</genre>
     <genre>4</genre>
     <genre>5</genre>
</category>
<category type='Type C'>
    <genre>6</genre>
</category>
</categories>

我将需要使用此XML的示例是类别类型B的所有流派.如何准确地过滤掉所有其他类别,以便可以执行 .find("genre").each. ... ,仅返回类型3、4和5.

An example of what i wouild need using this XML is all the genres for category type B. How exactly could i filter out all the other categories so i could do a .find("genre").each.... and return only genres 3,4 and 5.

在此处使用以下代码进行即时通讯:

Heres the code im using:

$.ajax({
        type: "GET",
        url: "myxml.xml",
        dataType: "xml",
        success: function(data){
            xml = data;
            });
        }
    });

    return {getXml : function() 
    { 
        if (xml) return xml;
    }};
})();

/* On Category Change */
$(".category").change(function(){
    var xml = xmlArtists.getXml();

    $(".genre").find("option").remove().end();

    var type = $(this).val();
    var typeGenres = $(xml).find("categories").filter(function(){
        return $(this).find("category").attr("type") == type;
    });

    typeGenres.find("genre").each(function(){
        var genre = $(this).text();
        $(".genre").append("<option value=" + genre + ">" + genre + "</option>");
    });
});

.category是一个对象,更改后的代码应触发...因为在更改时将另一个对象插入,但是无论如何这对这个问题并不重要...

The .category is an object when its changed the code should fire... since im poulating another object upon its change but anyways thats not much of importance for this question...

提前谢谢!

推荐答案

$(xml).find('category[type="Type B"] > genre')

这将找到type"Type B"的所有category节点,并获得每个匹配类别的所有genre子节点.

This would find all category nodes whose type is "Type B" and get all the genre child nodes of each matched category.

这篇关于jQuery:XML-从特定节点读取子节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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