最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

ajax - How to get an attribute value in plain text in an xml file using Javascript? - Stack Overflow

programmeradmin1浏览0评论

I have the following xml file hosted externally

<rsp stat="ok">
<feed id="" uri="">
<entry date="2012-08-15" circulation="154" hits="538" downloads="0" reach="30"/>
</feed>
</rsp>

How to import the xml document and get the value of "circulation" attribute in the "entry" tag using JavaScript?

I have the following xml file hosted externally

<rsp stat="ok">
<feed id="" uri="">
<entry date="2012-08-15" circulation="154" hits="538" downloads="0" reach="30"/>
</feed>
</rsp>

How to import the xml document and get the value of "circulation" attribute in the "entry" tag using JavaScript?

Share Improve this question asked Aug 16, 2012 at 19:28 RaghavendraRaghavendra 5,3874 gold badges38 silver badges53 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 3

You can get the xml file via Jquery ajax GET request and parse it like this:

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

Don't forget that, if there are more than one entry tag in xml, this will read all circulation attributes of those entries, so that you should be aware of how much circulation you want to process.

If you want to take only the first entry, you can use this:

$.ajax({
    type: "GET",
    url: "your_xml_file.xml",
    dataType: "xml",
    success: function(xml) {
        var circulation = $(xml).find('entry').first().attr("circulation");
    }
});

Here are my resources to write this:

http://api.jquery./first/

http://think2loud./224-reading-xml-with-jquery/

Here is an example:

    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');
发布评论

评论列表(0)

  1. 暂无评论