Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Querying XML in Javascript

I'm at a loss as to how to go about querying an XML file using Javascript. It's possible this isn't something XML is really suited for (i know a fully featured database might be a better option). I've looked into tools like XQuery, but I don't know how or if this is something I can use. Do browsers support XQuery? Can I write XQuery statements in Javascript files in such a way that I can use the results in other javascript functions? Any help would be appreciated.
Here is some context:

$.ajax({

    url: "http://api.wunderground.com/api/test.json",
    dataType: "jsonp",
    success: function (parsed_json) {
        //do stuff with json file
    $.ajax({
        type: "GET",
        url: "weather_map.xml",
        dataType: "xml",
        success: function(xml) {
            var value = $(xml).find('condition[name="Clear"]').text();
            alert(value);
                    // do stuff with XML file
        }
    });
        //do more stuff with json file
 });
like image 915
scifirocket Avatar asked Oct 20 '25 04:10

scifirocket


1 Answers

One of the easiest ways to process XML in JavaScript is to use jQuery. This is a very common JavaScript library which can be used to process XML files. For example

var xml = '<students><student name="bob" last="smith"/><student name="john" last="doe"/></students>';
var value = $(xml).find('student[name="bob"]').attr('last');
console.log(value);  // prints: smith

Nice Tutorial: http://www.switchonthecode.com/tutorials/xml-parsing-with-jquery

like image 161
JaredPar Avatar answered Oct 22 '25 18:10

JaredPar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!