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

I have the following xml file hosted externally<rsp stat="ok"><feed id=""

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');

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745456158a4628494.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信