gpt4 book ai didi

javascript - 在 Javascript 中查询 XML

转载 作者:行者123 更新时间:2023-11-28 00:22:47 25 4
gpt4 key购买 nike

我不知道如何使用 Javascript 查询 XML 文件。这可能不是 XML 真正适合的东西(我知道功能齐全的数据库可能是更好的选择)。我研究过像 XQuery 这样的工具,但我不知道如何使用或是否可以使用它。浏览器是否支持 XQuery?我可以在 Javascript 文件中编写 XQuery 语句,以便我可以在其他 javascript 函数中使用结果吗?任何帮助将不胜感激。
这是一些上下文:

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

最佳答案

在 JavaScript 中处理 XML 的最简单方法之一是使用 jQuery。这是一个非常常见的 JavaScript 库,可用于处理 XML 文件。例如

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

不错的教程:http://www.switchonthecode.com/tutorials/xml-parsing-with-jquery

关于javascript - 在 Javascript 中查询 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7809925/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com