views:

30

answers:

2

I have an xml file xyz.xml:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<legends>
<legend>
<number>1</number>
<contentString>abcd</contentString>
</legend>
<legend>
<number>2</number>
<contentString>efg</contentString>
</legend>
<legend>
<number>3</number>
<contentString>hij</contentString>
</legend>
</legends>

I am trying to read this with jQuery:

$(document).ready(function() {       

   $.get("xyz.xml",{},function(xml){

   var randomnumber=Math.floor(Math.random()*3); 


   $('legend',xml).each(function() {         

            if(randomnumber == $(this).find("number").text())
            {
          var c = "contentString";

          var legendStr = $(this).find(c).text();

          alert(legendStr);
            }               

   });
   });

 }); 

The jQuery code is not entering inside the function $('legend',xml).each(function().

Why this is happening.

+1  A: 

Have you already tried this?

$(xml).find("legend").each(function() {
   ...
});
john_doe
+2  A: 

If the server doesn't return a MIME type that jQuery can map to XML, it will make a wrong guess as to the type of the response. Specify the type of the data to keep it from guessing:

$.get("xyz.xml",{},function(xml){ ... },"xml");
Guffa