0

I've got a problem with using an external XML-file for my AJAX project. When I change the AJAX-variable XML to the content of the XML I get the right output. But the output of the code below is "undefined". What am I doing wrong?

html & AJAX code

<html>
<head><title>xml file in jquery</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
</head>
<body>

 <table>
 </table>

<script>


$(document).ready( function() {
    Dataextract();
    fetch();
});

function fetch() {

        setTimeout( function() {
        corporateData();
        fetch();
    }, 10000);

}

function Dataextract() {

    $.ajax({ 
    type: "GET",
     url: "test.xml",
     dataType: "xml",    
     success: function(xml) {


 $("table").children().remove();

      $(xml).find("feed").each( function() {

    var $entry = $(this);
       var mydata = $entry.find('w\\:entry').attr('w:date');

 document.write(mydata);

       $("table").append(info);

      });

     },
    }); 
};

</script>
</body>
</html>

My external XML-file ('test.xml') looks like this:

<rsp><feed><w:entry w:date="2012-08-15" w:circulation="154" w:hits="538" w:downloads="0" w:reach="30"/></feed></rsp>
1
  • 1
    You append an info variable, but it doesn't exist: $("table").append(info);
    – Christian
    Jun 23, 2014 at 22:46

2 Answers 2

0

I think the problem lies in trying to double-escape the ':'. Try this:

var mydata = $entry.find('w\:entry').attr('w\:date');

Either that or perhaps the problem is that you didn't escape the attribute name? I haven't used namespaces in Xml parsing with jQuery, but this may be another solution:

var mydata = $entry.find('w\\:entry').attr('w\\:date');

Alternatively if that doesn't help, try adding some debugging code and use console.log. I would start with this:

var mydata = $entry.find('w\:entry');
console.log(mydata);
console.log(mydata.length);

That will at least tell you if your find expression is working.

I'm not sure if you were going for a solution to this, but as someone mentioned in the comments, you also have some problems with what you're trying to append to that table.

First being, that as mentioned, you don't have the "info" variable declared. Second is that the only thing you are extracting from the Xml is a date (string). You aren't going to have any luck trying to append that to a $('table'). You would need to use either document.createElement to add a row + cells, or build an Html string containing that information.

ie:

jQuery('table').append(
    jQuery(document.createElement('tr')).append(
        jQuery(document.createElement('td')).text('your value')
    )
);

or

jQuery('table').append('<tr><td>yourvalue</td></tr>');
0

Seems like parsing this xml is tricky.

This works for me at least in Chrome: var mydata = $entry.find('entry').attr('w:date');

So you can leave off the w\\:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.