1

Can Javascript directly handle an xml file requested via AJAX. I have a server side xml file and need to populate fields from this xml. Can i say 'directly read "xmlfile.xml" (on server)' and then extract values in javascript from the response received and populate as required? Can you explain with example if possible?

3 Answers 3

3

If you can use jQuery, you can simply perform an XML AJAX call, and respond with the static file.

 <script type="application/javascript">
 $(function() {
     $('#get-xml a').click(function() {
         $.ajax({
             type: "GET",
             url: "xmlfile.xml",
             dataType: "xml",
             success: function(xml) {
                 $(xml).find('label').each(function(){
                     // your code. some example code bellow
                     var id_text = $(this).attr('id')
                     var name_text = $(this).find('name').text()

                     $('<li></li>')
                         .html(name_text + ' (' + id_text + ')')
                         .appendTo('#get-xml ol');
                 });
             }
         });
     });
 });
 </script>

Just be carefull:

Note: If you specify the dataType option described below, make sure the server sends the correct MIME type in the response (eg. xml as "text/xml"). Sending the wrong MIME type can lead to unexpected problems in your script.

10
  • @Stefan: It may well be easier, but the developer's convenience is not the only consideration. It's certainly not something the user is going to care about. And it's not a reason to downvote my answer either.
    – Tim Down
    Oct 16, 2009 at 0:16
  • I voted up the answer I believed deserved consideration and down the answer I believed did not warrant consideration. If more people use jQuery, more users will get cache hits when you link to google's ajax code repo. Furthermore, If you're processing large amounts of data, like, say, overly verbose xml files, javascript is not an issue. Do you also not use images on your websites? Any reasonably sized picture is going to land over 4x the size of the jQuery library. Oct 16, 2009 at 1:20
  • 1
    Your arguments hearken back to those of the early web; images should not be used, and all code should be optimized for speed. gzip compression, build minification, and widespread broadband adoption have made such concerns trivial, and even harmful. Yes, I think attempting to support IE6 is wrong and backward, and yes, I believe jQuery (or another library) should abstract away the pains of yesteryear. I have reasonable arguments, but I'll leave the rest off SO. If you wish to counter my points or describe your position further, write a blog entry and I'll read and comment. Prove me wrong. Oct 16, 2009 at 1:23
  • There are two separate arguments here: first, whether sites should support IE 6, about which I've made no comment, and second, whether jQuery or similar should be used for every scripting task, no matter how small. Since we haven't talked about the first point I'm going to leave it alone here. As to the second point, you are the one advocating using a particular tool or type of tool for all browser scripting tasks so I'd say the burden of proof is on you.
    – Tim Down
    Oct 16, 2009 at 9:04
  • @Stefan: Re the first of your two recent comments, my answer was perfectly valid. jQuery was not asked for in the question, and choosing to use jQuery is something you should make an informed decision about. Saying that jQuery is the only way to achieve what the OP wanted is clearly false; saying it's the best way is subjective and the dismissal of doing it without jQuery that your downvote represents is dogmatic and blinkered. The arguments about image size are irrelevant, and false: the kind of image you're talking about would be at least 76K, and I'd say that's more than reasonably sized.
    – Tim Down
    Oct 16, 2009 at 10:09
2

Yes. XMLHttpRequest has a responseXML property (populated once the request is completed in the usual way) which is a reference to an XML document. This has all the usual DOM methods and properties that you'd get in an HTML document. You do not need 50K of jQuery to achieve this.

1
1

As is the standard here on SO, I'm going to recommend using jQuery!

var myUrl = 'http://somesite.com/foo.xml';

function myXmlHandler(data){
    // do stuff with data, which is the contents of foo.xml
}

$.get(myUrl,{},myXmlHandler);

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.