views:

99

answers:

3

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  A: 

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.

voyager
This is easier than the accepted solution.
Stefan Kendall
@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
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.
Stefan Kendall
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.
Stefan Kendall
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
@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
@Tim Down: I offered a jQuery answer because it's a commonly used framework and people down the road might be directed here. Dogmatism is **always bad**. People saying *you have to always use a framework* don't take into account some real life implications and peoiple saying *you should never use a framework* are being overly simplistic and think that there are no differences cross browser. For that last point alone, I err on the side of *using* a framework, but have certainly worked without one. Having tried both ways, the ammount of problems solved by a framework are enourmous.
voyager
@voyager: Nothing wrong with your answer, and I regret adding "You do not need 50K of jQuery to achieve this" to mine, as it was unnecessary and provocative. As you may have guessed, I don't use libraries like jQuery: while I do see the value in them, I've been doing this stuff for a long time, know a lot of the browser issues and have built up my own loose collection of functions that I use instead, tailoring it to the particular requirements of what I'm working on.
Tim Down
@Tim Down: yours is a correct approach, but by using a jQuery you are *outsourcing* part of your development and testing of an important piece of code *for free*. Also, it's faster for a new developer to pick up jQuery based code than a custom made collection of functions.
voyager
@voyager: I'm not pushing my approach as being perfect for everyone; I don't think we really disagree. My situation is that I tend to work on JavaScript projects alone, and I trust my own code enough to prefer it to someone else's, but I understand most people's situations are very different.
Tim Down
@Tim Down: we aren't disagreeing :) We are exchanging our different point of view so that when some poor soul comes here could, may be, understand the pros and cons of both approaches.
voyager
+1  A: 

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);
inkedmn
+2  A: 

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.

Tim Down
correct.. I just saw this here http://www.w3schools.com/ajax/tryit.asp?filename=tryajax_httprequest_js4
Ajay