熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> PHP編程 >> 正文

PHP網絡開發詳解:XML文件的讀取[2]

2022-06-13   來源: PHP編程 
    ——此文章摘自《完全手冊PHP網絡開發詳解》定價 特價 詳細>>http://tracklinktechcn/?m_id=dangdang&a_id=A&l=&l_type= width= height= border= nosave>

    function ReqXml()                           //主程序函數
    {
        CreateXMLHttpRequest();                 //創建對象
        xmlobjonreadystatechange = StatHandler;    //判斷URL調用的狀態值並處理
        xmlobjopen(GET testxml true);   //調用testhtml
        xmlobjsend(null);                      //設置為不發送給服務器任何數據
    }
    function StatHandler()                      //用於處理狀態的函數
    {
        if(xmlobjreadyState == && xmlobjstatus == )                                                                  //如果URL成功訪問則輸出網頁
        {
            xml = xmlobjresponseXML;
            var allees = xmlgetElementsByTagName(employee);
                                          //獲取XML文件中的所有employee標簽
          
            documentgetElementById(xmlpage)innerHTML = ;//清空DIV層
            for(var i = ; i < alleeslength; i++)          //循環讀取每一個標簽
            {
                var tempobj name sex age band salary;
                tempobj = allees[i]getElementsByTagName(name);
                name = name: + tempobj[]childNodes[]nodeValue + <BR>;
                tempobj = allees[i]getElementsByTagName(sex);
                sex = sex: + tempobj[]childNodes[]nodeValue + <BR>;
                tempobj = allees[i]getElementsByTagName(age);
                age = age: + tempobj[]childNodes[]nodeValue + <BR>;
                tempobj = allees[i]getElementsByTagName(band);
                band = band: + tempobj[]childNodes[]nodeValue + <BR>;
                tempobj = allees[i]getElementsByTagName(salary);
                salary = salary: + tempobj[]childNodes[]nodeValue + <BR>;
                row = name + sex + age + band + salary + <HR>;
                documentgetElementById(xmlpage)innerHTML += row;                                                                    //將讀取結果放入DIV中
            }
        }
    }
    </script>
    </head>
    <body>
    <p><a href=# onclick=ReqXml();>Request XML page</a></p>
    <p><div id=xmlpage></div></p>
    </body>
    </html>

    運行結果如圖所示

http://developcsaicn/web/images/jpg>
  讀取XML文件

    上面的代碼首先讀取了XML文件中的所有employee標簽及其下的所有標簽來獲得XML文件中的全部內容然後使用頁面中的層(DIV)來輸出相應的內容

right>[http://developcsaicn/web/htm>]  []  


From:http://tw.wingwit.com/Article/program/PHP/201311/21449.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.