PHP & JavaScript Room :: 設置サンプル

実行結果

ソース

<script type="text/javascript">
function testXmlHttpRequestObjXML(xmlURL){
    /* XMLHttpRequestオブジェクト作成 */
    var xmlhttp=false;
    if(typeof ActiveXObject!="undefined"){ /* IE5, IE6 */
        try {
            xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); /* MSXML3 */
        }
        catch(e){
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); /* MSXML2 */
        }
    }
    if(!xmlhttp && typeof XMLHttpRequest!="undefined"){
        xmlhttp=new XMLHttpRequest(); /* Firefox, Safari, IE7 */
    }
    if(!xmlhttp){
        document.write("XMLHttpRequest非対応ブラウザ");
    }else{
        /* レスポンスデータ処理 */
        xmlhttp.onreadystatechange=function(){
            if(xmlhttp.readyState==4 && xmlhttp.status==200){

                /* ★レスポンスの返り値をXML形式(オブジェクト)で取得 */
                document["resXML"]=new Object();
                document.resXML=xmlhttp.responseXML;
                var s="";
                for(var i=0; document.resXML.childNodes[i]!=null; i++){
                    if(document.resXML.childNodes[i].nodeName=="ranking"){
                        var rankingNode=document.resXML.childNodes[i];
                        for(var j=0; rankingNode.childNodes[j]!=null; j++){
                            if(rankingNode.childNodes[j].nodeName=="item"){
                                var itemNode=rankingNode.childNodes[j];
                                for(var x=0; itemNode.childNodes[x]!=null; x++){
                                    var node=itemNode.childNodes[x];
                                    if(node.nodeName=="no" 
                                    || node.nodeName=="title" 
                                    || node.nodeName=="view" 
                                    || node.nodeName=="url"){
                                        s+=node.firstChild.nodeValue+"<br>";
                                    }
                                }
                            }
                        }
                    }
                }
                document.getElementById("resXmlHttpRequestObjXML").innerHTML="【XML形式】(XMLデータをオブジェクトに格納後、分解してHTML出力)<br>"+s;

                /* ★レスポンスの返り値をテキスト形式(文字列)で取得 */
                var resText=xmlhttp.responseText;
                document.getElementById("resXmlHttpRequestObjText").innerHTML="<p>【テキスト形式】<br><textarea style='width:450px; height:200px;'>"+resText+"</textarea></p>";

            }
        }
        /* HTTPリクエスト実行 */
        xmlhttp.open("POST",xmlURL,true);
        xmlhttp.send(null);
    }
}
window.onload=function(){
    /* ページ読み込み完了時に実行 */
    testXmlHttpRequestObjXML("./xml/ranking.xml");
}
</script>

<div id="resXmlHttpRequestObjXML"></div>
<div id="resXmlHttpRequestObjText"></div>

polarized women