我有以下外部托管的 xml 文件
<rsp stat="ok">
<feed id="" uri="">
<entry date="2012-08-15" circulation="154" hits="538" downloads="0" reach="30"/>
</feed>
</rsp>
如何使用JavaScript导入xml文档并获取“entry”标签中“circulation”属性的值?
最佳答案
您可以通过 Jquery ajax GET 请求获取 xml 文件并像这样解析它:
$.ajax({
type: "GET",
url: "your_xml_file.xml",
dataType: "xml",
success: function(xml) {
$(xml).find('entry').each(function(){
var circulation = $(this).attr("circulation");
// Do whatever you want to do with circulation
});
}
});
不要忘记,如果 xml 中有多个条目标记,这将读取这些条目的所有循环属性,以便您应该知道要处理多少循环。
如果您只想获取第一个条目,可以使用:
$.ajax({
type: "GET",
url: "your_xml_file.xml",
dataType: "xml",
success: function(xml) {
var circulation = $(xml).find('entry').first().attr("circulation");
}
});
以下是我撰写本文的资源:
关于javascript - 如何使用 Javascript 获取 xml 文件中纯文本的属性值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11994140/