Reputation: 1051
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
});
Upvotes: 4
Views: 9812
Reputation: 10582
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.net/days/07/06/e4x
Upvotes: 0
Reputation: 2156
Did you consider XQuery in the browser from http://xqib.org?
There is a nice demo there: http://xqueryguestbook.my28msec.com/
Upvotes: 0
Reputation: 754545
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
Upvotes: 4
Reputation: 1899
for $x in doc("books.xml")/bookstore/book
where $x/price>30
order by $x/title
return $x/title
Upvotes: 0