xml_response.xml:

<?xml version="1.0" encoding="utf-8"?>
<users>
    <user>
        <name>名字一</name>
        <pwd>密码一</pwd>
    </user>
    <user>
        <name>名字二</name>
        <pwd>密码二</pwd>
    </user>
</users>

xml_request.xml:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<script>
var xmlHttp;
function startRequest()
{
    if (window.ActiveXObject)
    {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else if (window.XMLHttpRequest)
    {
       xmlHttp = new XmlHttpRequest();
    }
    xmlHttp.onreadystatechange = handleStateChange;
    xmlHttp.open("GET","xml_response.xml",true);
    xmlHttp.send(null);
}
 
function handleStateChange()
{
    if (xmlHttp.readyState == 4)
    {
        if (xmlHttp.status == 200)
        {
            displayUsers();
        }
    }
}
 
function displayUsers()
{
    var xmlDoc = xmlHttp.responseXML;
    var userNodes = xmlDoc.getElementsByTagName("user");
    var row;
    var cell;
    for (i=0;i<userNodes.length;i++)
    {
        row = table1.insertRow();
        cell = row.insertCell();
        cell.align="center";
        cell.innerText = userNodes[i].childNodes[0].text;
        cell = row.insertCell();
        cell.align="center";
        cell.innerText = userNodes[i].childNodes[1].text;
    }
}
</script>
</head>

<body>
<input type="submit" name="Submit" value="点击获得用户信息" onClick="startRequest();" />
<table id="table1" width="300" border="0" cellspacing="0" cellpadding="0">
<tr>
    <th width="50%" scope="col">用户名</th>
    <th scope="col">密码</th>
</tr>
</table>
</body>
</html>

posted on 2009-05-05 09:33  雨季  阅读(295)  评论(0)    收藏  举报