如何使用Javascript在xml文件中以纯文本形式获取属性值?

如何使用Javascript在xml文件中以纯文本形式获取属性值?,javascript,ajax,xml-parsing,Javascript,Ajax,Xml Parsing,我在外部托管了以下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”属性的值?以下是一个示例: if (w

我在外部托管了以下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”属性的值?

以下是一个示例:

    if (window.XMLHttpRequest) {
      xhttp=new XMLHttpRequest();
    } else {
      xhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xhttp.open("GET","the name of your xml document.xml",false);
    xhttp.send();
    xmlDoc=xhttp.responseXML;
    var circulation = xmlDoc.getElementsByTagName("entry")[0].getAttribute('circulation');
以下是一个例子:

    if (window.XMLHttpRequest) {
      xhttp=new XMLHttpRequest();
    } else {
      xhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xhttp.open("GET","the name of your xml document.xml",false);
    xhttp.send();
    xmlDoc=xhttp.responseXML;
    var circulation = xmlDoc.getElementsByTagName("entry")[0].getAttribute('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");
    }
});
以下是我写这篇文章的参考资料:


您可以通过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");
    }
});
以下是我写这篇文章的参考资料: