1

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.

2 Answers 2

2

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");
Sign up to request clarification or add additional context in comments.

Comments

1

Have you already tried this?

$(xml).find("legend").each(function() {
   ...
});

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.