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
5 Answers
Reset to default 4One 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
版权声明:本文标题:html - Querying XML in Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741697890a2393113.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论