admin管理员组

文章数量:1392099

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

The XML file structure:

<?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>

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>");
    });
});

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...

Thanks in advance!!

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

The XML file structure:

<?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>

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>");
    });
});

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...

Thanks in advance!!

Share Improve this question asked Mar 8, 2012 at 23:59 OrlandoOrlando 9752 gold badges20 silver badges45 bronze badges 1
  • Just figured it out was easier than i thought... heres the code: /* On Category Change */ $(".category").change(function(){ var xml = xmlArtists.getXml(); $(".genre").find("option").remove().end(); var type = $(this).val(); $(xml).find("category[type='" + type + "']").find("genre").each(function(){ var genre = $(this).text(); $(".genre").append("<option value=" + genre + ">" + genre + "</option>"); }); }); – Orlando Commented Mar 9, 2012 at 0:04
Add a ment  | 

1 Answer 1

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

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

本文标签: javascriptjQueryXMLReading child nodes from a specific nodeStack Overflow