JS操作XML執行個體

來源:互聯網
上載者:User

        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">   
        <html xmlns="http://www.w3.org/1999/xhtml">   
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />   
        <title>JS讀取XML相容IE/FF/Chrome,Safari - www.cxybl.com</title>   
        <style>
        li{list-style:none;}   
        </style>
        <script type="text/javascript">
        var orderDoc;   
        //擷取xml檔案   
        function loadXmlFile(xmlFile){   
          var xmlDom = null;   
          if (window.ActiveXObject){   
            xmlDom = new ActiveXObject("Microsoft.XMLDOM");   
            xmlDom.async=false;   
           xmlDom.load(xmlFile)xmlDom.loadXML(xmlFile);//如果用的是XML字串//如果用的是xml檔案   
          }else if (document.implementation && document.implementation.createDocument){   
            var xmlhttp = new window.XMLHttpRequest();   
            xmlhttp.open("GET", xmlFile, false);   
            xmlhttp.send(null);   
            xmlDom = xmlhttp.responseXML;   
          }else{   
            xmlDom = null;   
          }   
          return xmlDom;   
        }   
        //判斷子節點為不為空白   
        function isnull(obj)   
        {   
            var nodevalue = "";   
            if(obj.childNodes[0] != null)   
            {   
                nodevalue =obj.childNodes[0].nodeValue;   
            }   
            return nodevalue;   
        }   
        var stringsss="";   
        //根據編號擷取資料   
        function getDataByid(number)   
        {   
            var time =isnull(orderDoc.getElementsByTagName("time")[number]);   
            var place =isnull(orderDoc.getElementsByTagName("place")[number]);   
            var reason =isnull(orderDoc.getElementsByTagName("reason")[number]);   
            var unit = isnull(orderDoc.getElementsByTagName("unit")[number]);   
            var money =isnull(orderDoc.getElementsByTagName("money")[number]);   
            var status =isnull(orderDoc.getElementsByTagName("status")[number]);   
            var docNum =isnull(orderDoc.getElementsByTagName("docNum")[number]);   
            var score =isnull(orderDoc.getElementsByTagName("score")[number]);   
            var std='<li><b>網址:'+(time)+'<br />站名稱:'+(place)+'<br />描述:'+(reason)+'</b></li>';   
            return std;   
        }
        //獲得頁面內容   
        function getContent(){   
            //exBrows();   
            orderDoc=loadXmlFile("http://www.ok22.org/download/ex2.xml");   
                var items=orderDoc.getElementsByTagName("results").length;   
                //alert(orderDoc.getElementsByTagName("Item").length);   
                var htmlstr="";   
                stringsss+='<ul>';   
                for(i=0;i<items;i++){   
                    stringsss+=getDataByid(i);   
                }   
                stringsss+='</ul>'   
                document.write(stringsss);   
        }
        </script>
        </head>
        <body>   
        </body>   
        <script type="text/javascript" language="javascript">   
        getContent();   
        </script>   
        </html>

因為XML有可能為空白所以添加了一個判斷節點是否為空白!
        //判斷子節點為不為空白   
        function isnull(obj)   
        {   
            var nodevalue = "";   
            if(obj.childNodes[0] != null)   
            {   
                nodevalue =obj.childNodes[0].nodeValue;   
            }   
            return nodevalue;   
        }



相關文章

Cloud Intelligence Leading the Digital Future

Alibaba Cloud ACtivate Online Conference, Nov. 20th & 21st, 2019 (UTC+08)

Register Now >

Starter Package

SSD Cloud server and data transfer for only $2.50 a month

Get Started >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。