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