renilative
renilative

Reputation: 159

How do I loop through a returned XML object in jQuery?

I have this xml and i want to return the country ID where CountryName is = Namibia

 <Countries>
- <Country>
  <CountryName>Zimbabwe</CountryName> 
  <CountryID>1</CountryID> 
  </Country>
- <Country>
  <CountryName>South Africa</CountryName> 
  <CountryID>2</CountryID> 
  </Country>
- <Country>
  <CountryName>Namibia</CountryName> 
  <CountryID>3</CountryID> 
  </Country>
- <Country>
  <CountryName>Nigeria</CountryName> 
  <CountryID>4</CountryID> 
  </Country>
</Countries>

I am using this code but it is just returning 0 , How do i make this work???

$.ajax({
        type:"GET",
        url : "countriesxml.php",
        dataType: "xml",
        success: function(xml){
            $(xml).find("Country").each(function(){
                var cid = 0;
                if($(this).find("CountryName").text()==cname)
                {
                    cid = $(this).find("CountryID").text();

                 }
                 else
                 {
                    cid = 0;
                 }
                 alert(cid);

             });
         }
 });

Upvotes: 0

Views: 142

Answers (1)

Amr Elgarhy
Amr Elgarhy

Reputation: 68902

I found some articles talking about this issue:
http://think2loud.com/reading-xml-with-jquery/
http://marcgrabanski.com/articles/jquery-makes-parsing-xml-easy
http://api.jquery.com/jQuery.parseXML/

But I didn't this before my self.

Upvotes: 1

Related Questions