admin管理员组

文章数量:1303429

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: ".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
 });

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./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
 });
Share edited Oct 18, 2011 at 16:21 scifirocket asked Oct 18, 2011 at 15:39 scifirocketscifirocket 1,0511 gold badge16 silver badges23 bronze badges 1
  • #1 hit on google is w3schools./xml/xml_parser.asp. Is there something more specific you are looking to do? – jbabey Commented Oct 18, 2011 at 15:41
Add a ment  | 

5 Answers 5

Reset to default 4

One of the easiest ways to process XML in JavaScript is to use jQuery. This is a very mon 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./tutorials/xml-parsing-with-jquery

for $x in doc("books.xml")/bookstore/book
where $x/price>30
order by $x/title
return $x/title

Have a look at http://www.w3schools./dom/dom_loadxmldoc.asp

Did you consider XQuery in the browser from http://xqib?

There is a nice demo there: http://xqueryguestbook.my28msec./

E4X support is in some browsers, but I don't know how wide the coverage is. It's not xquery, but it is a very natural way of processing xml data in javascript.

var x=new XML("<root><el>hello, world</el></root>");
alert(x.el);

A good guide to E4X is http://rephrase/days/07/06/e4x

本文标签: htmlQuerying XML in JavascriptStack Overflow