1

I have a XML file and am trying to pull out data out of it. The XML file looks like this

    <doc>

    <str name="name">Rocky</str>
    <str name="Last_name">balboa</str>
    <str name="age">42</str>
    <str name="sex">M</str>
    <str name="dob">2012-09-09</str>

   </doc>
   <doc>... </doc>
   <doc>... </doc>
   <doc>... </doc>

My .ajax call goes like this...

    $.ajax({
            type : "GET",
            url : "my.xml",
            dataType : "xml",
            success : function(data) {
                $(data).find('doc').each(function() {
                    alert("i was here");
            var u1 = $(this).find('name').text();alert(u1);
            var u2 = $(this).find('last_name').text();
            var finale1 = u1 + "/" + u2;
            var dt = $(this).find('dob').text();
            var dt1 = dt.substr(0,4);
            var desc = $(this).find('age').text();
                    alert("i am here");

                });
            }   

            });

What am I doing wrong over here? Can anyone please point out.

3
  • whats going wrong.. please elaborate your question... Commented Oct 5, 2011 at 6:44
  • its very difficult to detect the error without your help can you please point out where are you getting the error or what is the expected behavior that is not achieved Commented Oct 5, 2011 at 6:46
  • #Varun. as you see alert over there, its coming up empty. Commented Oct 5, 2011 at 6:47

4 Answers 4

1

When you are trying to select the following tag:

<str name="name">Rocky</str>

Instead of using $(this).find('name') you should use $(this).find('str[name="name"]')

This error appears many times, for each str tag.

Sign up to request clarification or add additional context in comments.

Comments

1

You should parse your xml before using it (no need to do it if your ajax call returns xml). Pay attention to:

  1. Tag names: you look for a <document> element whereas you have <doc> elements
  2. Attributes and tag are different things. find('name') looks for a tag, not for a name attribute:

See here for a working example (My xml is a local string, but you can easily adapt the script) and here for parseXML documentation and xml usage examples.

var xml = "<doc><str name=\"name\">Rocky</str><str name=\"sex\">M</str><str name=\"dob\">2012-09-09</str></doc>",
    xmlDoc = $.parseXML( xml ),
    xml = $( xmlDoc ),
    name = xml.find( "str[name='name']" );

alert (name.text()); 

Comments

0

You can use Jquery parse xml to navigate the dom e.g. http://jsfiddle.net/qd2xY/

var xml = '<docs><doc><str name="name">Rocky</str><str name="Last_name">balboa</str><str name="age">42</str><str name="sex">M</str><str name="dob">2012-09-09</str></doc><doc><str name="name">Rocky1</str></doc></docs>';


$(document).ready(function(){
    xmlDoc = $.parseXML(xml);
    $xml = $( xmlDoc ),    
    $xml.find('doc').each(function(){
       alert($(this).find('str[name="name"]').text())
    })
})

Comments

0

your xml is not well formed, also never use the DOM traversal methods to parse the XML it becomes browser dependent, always use some sort of standard parser e.g. in jquery you can use .parseXML, in you success call back try

 success : function(data) {
    var xml=data;
    xmlDoc = $.parseXML( xml ),
    $xml = $( xmlDoc );

    $.each($xml.find("str"),function(){
    alert($(this).attr("name"));
    });
}

DEMO

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.