jQuery parsing XML: get an element with a specific attribute

73,348

Solution 1

To answer the specific questions, "How can I pass this argument to function parserCardsXml(xml)?"

function loadCards(lang)
{
    $.ajax({
        type: "GET",
        url: 'data/english.xml',
        dataType: "xml",
        success: function (xml) { parseCardsXml(xml, lang); }
    });
}

And, "How can I get name and description with a specific attribute?"

function parseCardsXml(xml, lang)
{
    var $xml = $(xml),
        name = $xml.find('name[lang="' + lang + '"]').text(),
        desc = $xml.find('desc[lang="' + lang + '"]').text();
}

Solution 2

var xml='<cards>\
        <card id="3">\
          <name lang="es"></name>\
          <description lang="es"></description>\
          <name lang="en"></name>\
          <description lang="en"></description>\
        </card></cards>';

and the jquery part

$(xml).find('Card').each(function(i,j)
    {           
       console.log($(j).attr("id"));
       console.log($(j).find("name").attr("lang"));

    });

http://www.jsfiddle.net/VZjmV/6/

Solution 3

$(xml).find('name[lang="en"], description[lang="en"]') should do the trick

Edit: More complete answer

$(xml).find('card').each(function () {
  var id, name, description;
  id          = $(this).attr('id'); // or just `this.id`
  name        = $(this).children('name[lang="en"]').text();
  description = $(this).children('description[lang="en"]').text();
  // do something with the id, name, and description
});
Share:
73,348
VansFannel
Author by

VansFannel

I'm software architect, entrepreneur and self-taught passionate with new technologies. At this moment I am studying a master's degree in advanced artificial intelligence and (in my free time ) I'm developing an immersive VR application with Unreal Engine. I have also interested in home automation applying what I'm learning with Udacity's nanodegree course in object and voice recognition.

Updated on July 05, 2022

Comments

  • VansFannel
    VansFannel almost 2 years

    I'm developing an HTML5 application.

    I want to parse an XML like this one:

    <?xml version="1.0" encoding="utf-8" ?>
        <cards>
            ...
            <card id="3">
              <name lang="es"></name>
              <description lang="es"></description>
              <name lang="en"></name>
              <description lang="en"></description>
            </card>
            ...
        </cards>
    

    I want to get the name and description that have attribute lang="en".

    I start writing code, but I don't know how to finish it:

    function loadCards(lang)
    {
        $.ajax({
            type: "GET",
            url: 'data/english.xml',
            dataType: "xml",
            success:parseCardsXml
        });
    }
    
    function parseCardsXml(xml)
    {
        $(xml).find('Card').each(function()
        {
            var id = $(this).attr('id');
            var name = $(this).find('name');
        }
    }
    

    By the way, loadCards function has an argument (or parameter) called lang.

    How can I pass this argument to function parserCardsXml(xml)? How can I get name and description with a specific attribute?

  • VansFannel
    VansFannel over 12 years
    Thanks for your answer but, how can I set var name and var description with values found?
  • Flambino
    Flambino over 12 years
    @VansFannel Updated my answer