2

次の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」属性の値を取得するにはどうすればよいですか?

4

2 に答える 2

3

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");
    }
});

これを書くための私のリソースは次のとおりです。

http://api.jquery.com/first/

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

于 2012-08-16T19:37:50.617 に答える
1

次に例を示します。

    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');
于 2012-08-16T19:36:36.993 に答える