admin管理员组文章数量:1426058
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 02 Answers
Reset to default 3You 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');
本文标签: ajaxHow to get an attribute value in plain text in an xml file using JavascriptStack Overflow
版权声明:本文标题:ajax - How to get an attribute value in plain text in an xml file using Javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745456158a2659110.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论