ajax解析XML文件

来源:互联网 发布:大连软件企业 编辑:程序博客网 时间:2024/05/21 17:33

XML代码:

<?xml version="1.0" encoding="UTF-8"?>    <user>      <uname>张三</uname>      <sex>男</sex>    <age>20</age>  </user>  

HTML代码:

<?xml version="1.0" encoding="utf-8"?><!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" xml:lang="zh" lang="zh" dir="ltr"><head profile="http://www.w3.org/2000/08/w3c-synd/#">    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />    <meta name="keywords" content="Leo,HEnT Studio,LAMP,PHP,MySQL" />    <meta name="DEscription" content="HEnT Studio" />    <meta name="Author" content="Leo,HEnT Studio" />    <title>AJAX</title>    <link rel="shortcut icon" href="favicon.ico" /><script type="text/javascript"><!--    var xmlHttp;    try    {        //Firefox Opera 8.0+ Safari        xmlHttp = new XMLHttpRequest();    }    catch (e)     {        //Internet Explorer 6.0+        try         {            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");        }        catch (e)         {            //Internet Explorer 5.5+            try             {                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");            }            catch (e)             {                alert("Your browser does not support AJAX");            }                    }    }    xmlHttp.onreadystatechange = processRequest;    xmlHttp.open("get","/test.xml",true);    xmlHttp.send(null);    function processRequest()    {       var strHTML="正在加载数据……";       if (xmlHttp.readyState != 4) { // 判断对象状态       document.getElementById('top_user').innerHTML="正在加载数据……";       }      else       {        if (xmlHttp.status == 200) { // 信息已经成功返回,开始处理信息           xmlObj = xmlHttp.responseXML;           document.getElementById('top_user').innerHTML=xmlObj.getElementsByTagName("uname")[0].firstChild.nodeValue;         }             else              { //页面不正常           document.getElementById('top_user').innerHTML="您所请求的页面有异常,请稍候再试。";         }      }    }//--></script></head><body><div class="topnavright" id="top_user"></div></body></html>