次の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」属性の値を取得するにはどうすればよいですか?
次の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」属性の値を取得するにはどうすればよいですか?
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");
}
});
これを書くための私のリソースは次のとおりです。
次に例を示します。
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');